小编KVr*_*ron的帖子

为什么需要保留NSString变量?

我的.h文件中有以下代码:

@interface Utils : NSObject {
    NSString *dPath;
}    
@property(nonatomic, retain) NSString *dPath;
Run Code Online (Sandbox Code Playgroud)

在我的.m文件中:

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
dPath = [[documentPaths objectAtIndex:0] stringByAppendingPathComponent:kDatabaseName];
[dPath retain]; 
Run Code Online (Sandbox Code Playgroud)

如果dPath已经定义为(非原子,保留),为什么还要保留 dPath?如果我不添加[dPath retain]; 我得到一些奇怪的,随机的错误,并且在其他函数中使用此变量时应用程序崩溃.我想这是因为某些自动释放,但我没有.

那么,什么是(非原子的,保留)做什么呢?是否真的有必要[dPath保留]; 或者我只是隐藏其他东西?

iphone objective-c autorelease nsstring retain

2
推荐指数
1
解决办法
351
查看次数

标签 统计

autorelease ×1

iphone ×1

nsstring ×1

objective-c ×1

retain ×1