我使用了下面的代码。
主视图.h:
NSString *sCopySource;
NSString *sFileSource;
// retain and copy used both for test proposes
@property (nonatomic, retain) NSString *sCopySource;
@property (nonatomic, copy) NSString *sFileSource;
主视图.m:
// Inside the first method:
sCopySource = [NSString stringWithFormat:@"%@%@", path1, filename];
sFileSource = [NSString stringWithFormat:@"%@%@", path2, filename];
// Inside the second method:
[[NSFileManager defaultManager] copyItemAtPath:sCopySource toPath:sFileSource error:&err];
并在代码的最后一行通过启用僵尸的对象 sCopySource
和 sFileSource
出错:
message sent to deallocated instance
为什么?标记为retain
和copy
的属性。如何解决这个问题?
非常感谢您的帮助!
附言请不要回答使用ratain
和release
方法。它们非常不方便。
最佳答案
您已经定义了属性,但是您正在直接写入实例变量。
如果要在属性中使用retain/release逻辑,需要使用:
self.sCopySource = [NSString stringWithFormat:@"%@%@", path1, filename];
self.sFileSource = [NSString stringWithFormat:@"%@%@", path2, filename];
这样,就使用了复制和保留的方法。
关于iphone - 僵尸报告 : message sent to deallocated instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9505363/