带有动态属性的点符号

时间:2011-08-01 23:20:17

标签: iphone objective-c ipad

我有以下用于核心数据存储的类。

@interface CVMessage : NSManagedObject {
    NSString * message;
}

@property (nonatomic, retain) NSNumber * date_created;
@property (nonatomic, retain) NSString * message;
@property (nonatomic, retain) NSNumber * mid;
@property (nonatomic, retain) CVLogin * creator;
@property (nonatomic, retain) NSSet * stars;
@property (nonatomic, retain) NSSet * embeds;
@property (nonatomic, retain) CVTopic * topic;
@property (nonatomic, assign) BOOL options;

@end

@implementation CVMessage
@synthesize options;
@dynamic message;
@dynamic mid;
@dynamic creator;
@dynamic date_created;
@dynamic embeds;
@dynamic stars;
@dynamic topic;

我希望能够做到这样的事情:

CVMessage * mes = [CVMessage new];  mes.message = @“some text”;

我的直觉是我必须为此实现我自己的setter,所以我有:

-(void)setMessage:(NSString *) newMessage
{
    if (self.message != newMessage)
    {
        [self.message release];
        [newMessage retain];
        self.message = newMessage;
    }
}

当我运行它时,这给了我一个错误。我错过了什么吗?

1 个答案:

答案 0 :(得分:3)

self.message = newMessage[self setMessage:newMessage]相同,因此您可能会在那里获得无限递归,并且错误可能是堆栈溢出。

直接访问变量:

-(void)setMessage:(NSString *) newMessage
{
    if (self.message != newMessage)
    {
        [message release];
        message = [newMessage retain];
    }
}