Alex Rivera | Logout

iOS Setters and Getters and Underscored Property Names

Asked 2013-06-10T04:53:23.503
8

So I have a NSString property named description, defined as follows:

@property (strong, nonatomic) NSMutableString *description;

I'm able to refer to it as _description when I define the getter, as follows:

- (NSString *)description
{
    return _description;
}

However, when I define a setter, as follows:

-(void)setDescription:(NSMutableString *)description
{
    self.description = description;
}

It breaks _description from the aforementioned getter (undeclared identifier). I know I can probably just use self.description instead, but why does this happen?

Edit
Report

1 Answer

10

1) NSObject already has a method named description. Pick another name

2) Your setter is an infinite loop

But as to your actual question: The compiler will only autogenerate backing variables if you do not override both methods.

P.S. No, you can't just "use self.description instead" because then your getter would also be an infinite loop.

answered 2013-06-10T05:02:01.163

Your Answer