【发布时间】:2011-02-05 06:34:09
【问题描述】:
我的 .h 文件中有以下代码:
@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];
如果它已经定义为(非原子,retain),为什么我必须retain dPath? 如果我不添加 [dPath 保留];在其他函数中使用此变量时,我得到一些奇怪的随机错误并且应用程序崩溃。我猜这是因为这里有一些 autorelease 但我没有。
那么,(nonatomic, retain) 到底在做什么呢? [dPath 保留] 真的有必要吗?还是我只是在隐藏其他东西?
【问题讨论】:
-
如果您在 Utils 中设置 dPath,请确保您使用的是 self.dPath,而不仅仅是 dPath
标签: iphone objective-c nsstring retain autorelease