我有数据对象类:
@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/