我有以下簡單的類定義:目標C類屬性變量
//mycommon.h
@interface CurrentPath : NSObject
@property (nonatomic, strong) NSString* PathString;
@property (nonatomic, strong) NSMutableArray* PathArr;
- (void) addAddressToPath:(NSString*) address;
@end
//mycommon.m
@implementation CurrentPath : NSObject
@synthesize PathString;
@synthesize PathArr;
- (void) addAddressToPath:(NSString*) address{
NSLog(@"addAddressToPath...");
// Add to string
self.PathString = [self.PathString stringByAppendingString:address];
// Add to Arr
[self.PathArr addObject:address];
}
@end
在另一類我做#import<mycommon.h>
並聲明像這樣的變量:
@interface myDetailViewController :
{
CurrentPath* currentPath;
}
- (void) mymethod;
@end
和
@implementation myDetailViewController
- void mymethod{
self->currentPath = [[CurrentPath alloc] init];
NSString* stateSelected = @"simple";
[self->currentPath addAddressToPath:stateSelected];
}
@end
問題是,自我的PathString和PathArr性能> currentPath此方法調用,我認爲應該有「簡單」,在他們之後是空的。請幫忙!
你在哪裏分配內存到PathArr? – rishi
hmm。我認爲@synthesize應該這樣做。可以說,它並不是實例函數addAddressToPath應該填充PathString。但是當我嘗試在方法調用後打印它時,它會打印出null。不應該是「簡單」。 – shaffooo
不@synthesize不會那樣做,您需要首先檢查合成生成的內存管理規則和定義。 – rishi