我在的.h文件下面的代码:为什么NSString变量需要保留?
@interface Utils : NSObject {
NSString *dPath;
}
@property(nonatomic, retain) NSString *dPath;
在我的.m文件:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
dPath = [[documentPaths objectAtIndex:0] stringByAppendingPathComponent:kDatabaseName];
[dPath retain];
为什么我要保留dPath如果它已被定义为(非原子,保留)? 如果我不添加[dPath retain];我收到了一些奇怪的随机错误,并在其他函数中使用此变量时应用程序崩溃。我想这是因为一些autorelease somehere,但我没有任何。
那么,什么是(非原子,保留)做呢?是否真的有必要[dPath retain];或者我只是用这个隐藏别的东西?
如果您在Utils中设置了dPath,请确保您使用的是self.dPath,而不仅仅是dPath – anq 2011-02-05 06:36:54