iphone - 僵尸报告 : message sent to deallocated instance

标签 iphone objective-c ios memory-management nsstring

我使用了下面的代码。

主视图.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];

并在代码的最后一行通过启用僵尸的对象 sCopySourcesFileSource 出错:

message sent to deallocated instance

为什么?标记为retaincopy 的属性。如何解决这个问题?

非常感谢您的帮助!

附言请不要回答使用ratainrelease 方法。它们非常不方便。

最佳答案

您已经定义了属性,但是您正在直接写入实例变量。

如果要在属性中使用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/

相关文章:

objective-c - 如何知道当前 UITableView 单元格相对于 swift 3.0 中的 UIViewController 的位置?

objective-c - 如何在 iOS 中的两个 View 之间传递变量

ios - 关闭具有多个文本字段的第一响应者/键盘

objective-c - iOS如何实现一个协议(protocol)的@property

iphone - 在 UITableview 中选择多行

ios - Xcode 粒子发射器编辑器不使用 ~ipad 图像?

ios - 如何支持带有蓝牙键盘的 UIWebView 上的箭头键?

iphone - 能保证一张照片在后台上传吗?

objective-c - didReceiveMemoryWarning - 如何区分警告级别

ios - 多行自动收缩标签 Swift