我使用了以下代码。
MainView.h:
NSString *sCopySource;
NSString *sFileSource;
// retain and copy used both for test proposes
@property (nonatomic, retain) NSString *sCopySource;
@property (nonatomic, copy) NSString *sFileSource;
MainView.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
的属性。如何解决这个问题?
非常感谢您的帮助!
P.S。请勿回答使用ratain
和release
方法。他们非常不方便。
答案 0 :(得分:2)
您已定义了该属性,但您正在直接写入实例变量。
如果要在属性中使用retain / release逻辑,则需要使用:
self.sCopySource = [NSString stringWithFormat:@"%@%@", path1, filename];
self.sFileSource = [NSString stringWithFormat:@"%@%@", path2, filename];
这样,使用了复制和保留的方法。