2

I have data object class:

@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

I can set the title property just fine when using non-synthesized methods, but when I set a property with synthesized accessors I get an error in the updateText method on the line that reads:

self.text=@"new text";

The error is:

*** 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

Why do identical non-synthesized accessors work and synthesized ones don't?

The object is created in main thread and Error appears when it's accessed from NSOperation thread.

4

3 回答 3

3

设置器应该这样编码:

[title autorelease]
title = [aString copy];

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

或者从Cocoa 的内存管理编程指南中选择任何其他一致的访问器样式

于 2009-07-11T09:48:51.907 回答
0

In this code [self setTitle:[self title]] would release and dealloc title before copying it. You need to check if title == aString in the setter.

于 2009-07-11T18:57:51.403 回答
0

您发布的代码对我来说很好。此代码与您使用的实际代码之间有什么不同吗?

您看到的错误消息引用了“僵尸”,它们是指向已释放对象的指针。此代码中没有任何内容显示此类行为的任何风险,这使我认为实际错误在其他地方。

一种可能的解决方案是使用 Xcode 的调试器来查看NSString对象的地址,并使用该信息来确定最终导致NSInvocation警告的对象。

于 2009-07-11T05:48:33.467 回答