开发者

dot notation with dynamic property

I have the following class that I use for a core data storage.

@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;
@dynam开发者_C百科ic creator;
@dynamic date_created;
@dynamic embeds;
@dynamic stars;
@dynamic topic;

I would like to be able to do something like:

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

my intuition is that I'd have to implement my own setter for this, so I have:

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

This gives me an error when I run it. Am I missing something?


self.message = newMessage is the same as [self setMessage:newMessage], so you are probably getting infinite recursion there, and the error is probably an stack overflow.

Just access the variable directly:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜