objective-c - 为什么我的程序在使用 self.property 访问属性时会崩溃?和一个合成的访问器?

标签 objective-c cocoa memory zombie-process

我有数据对象类:

@interface Item: NSObject {
    NSString *title;
    NSString *text;
}

@property (copy) NSString *title;
@property (copy) NSString *text;

@end

@implementation Item

@synthesize text;

- (void)updateText {
    self.text=@"new text";
}

- (NSString *)title {
    return title;
}

- (void)setTitle:(NSString *)aString {
    [title release];
    title = [aString copy];
}

@end

使用非合成方法时,我可以很好地设置 title 属性,但是当我使用合成访问器设置属性时,我在 updateText 方法中收到错误该行内容如下:

self.text=@"new text";

错误是:

*** NSInvocation: warning: object 0x462d2c0 of class '_NSZombie_CFString' does not implement methodSignatureForSelector: -- trouble ahead
*** NSInvocation: warning: object 0x462d2c0 of class '_NSZombie_CFString' does not implement doesNotRecognizeSelector: -- abort

为什么相同的非综合访问器可以工作,而综合访问器却不能?

该对象是在主线程中创建的,从 NSOperation 线程访问该对象时会出现错误。

最佳答案

setter 应该这样编码:

[title autorelease]
title = [aString copy];

否则另一个线程可能会在其脚下释放一个标题对象。

或者从Memory Management Programming Guide for Cocoa中选择任何其他一致的访问器样式

关于objective-c - 为什么我的程序在使用 self.property 访问属性时会崩溃?和一个合成的访问器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1112971/

相关文章:

matlab - 是否可以为 matlab 构建一个大内存系统?

objective-c - IconRef 来自的文件

xcode - 有没有办法在没有 NSClassFromString 的情况下使用 GrowlApplicationBridge?

c++ - 从记事本进程内存中读取文本

iOS NSUserDefaults 同步完成前的访问

macos - 在 10.11 中以编程方式从 Assets 目录中获取颜色?

c - 在C中,函数是在第一次调用时加载到内存中还是在程序启动时加载到内存中?它可以从内存中卸载吗?

ios - 调整 UITextfield 宽度

objective-c - 了解 Cocoa 应用程序中的 gc-roots 信息

objective-c - Objective C 代码是否有 github markdown 语言标识符?