我有数据对象类:
@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线程访问时会出现错误。
发布于 2009-07-11 09:48:52
setter应该这样编码:
[title autorelease]
title = [aString copy];否则,另一个线程可能会在其脚下释放一个title对象。
或者从Memory Management Programming Guide for Cocoa中选择任何其他一致的访问器样式。
发布于 2009-07-11 05:48:33
你发布的代码对我来说工作得很好。这段代码和你实际使用的代码有什么不同吗?
您看到的错误消息引用了“僵尸”,即指向已经释放的对象的指针。这段代码中没有显示任何此类行为的风险,这让我认为实际的错误在其他地方。
一种可能的解决方案是使用Xcode的调试器来查看NSString对象的地址,并使用该信息来确定哪个对象最终会导致NSInvocation警告。
发布于 2009-07-11 18:57:51
在这段代码中,[self setTitle:[self title]]将在复制title之前释放和取消分配它。您需要检查设置器中是否有title == aString。
https://stackoverflow.com/questions/1112971
复制相似问题