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?

2
  • It is because you are overriding description method already available in NSObject class. Try using some other name for your property.
    – Mrunal
    Jun 10, 2013 at 5:03
  • 3
    Also change your getter return type to similar to what your property declares.
    – Zen
    Jun 10, 2013 at 5:10

2 Answers 2

16

@borrrden 's answer is very good. I just want to add some details.

Properties are actually just syntax sugar. So when you declare a property like you did:

@property (strong, nonatomic) NSMutableString *description;

It is automatically synthesized. What it means: if you don't provide your own getter + setter (see borrrden's answer), an instance variable is created (by default it has name "underscore + propertyName"). And getter + setter are synthesized according to the property description that you provide (strong, nonatomic). So when you get/set the property, it is actually equal to calling the getter or the seter. So

self.description;

is equal to [self description]. And

self.description = myMutableString;

is equal to [self setDescription: myMutableString];

Therefore when you define a setter like you did:

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

It causes an infinite loop, since self.description = description; calls [self setDescription:description];.

7
  • Is it unnecessary to write @synthesize description = _description; then?
    – muttley91
    Jun 10, 2013 at 5:25
  • 1
    You can simply declare a property. Then an instance variable, getter and setter will be automatically created for you. But if you declare your own getter and setter, then instance variable is not created. So you should either @synthesize it or declare it manually in the @interface { NSMutableString *_description}. But it is a rare option nowadays :) If you need both custom getter and setter, you can use @synthesize. Otherwise you may skip it. And actually, if you don't implement some custom behaviour, you can skip getter and setter too. @property declaration is enough. Jun 10, 2013 at 5:36
  • Thanks! So basically, if I don't have any custom behaviour for setting/getting, I can just declare it. Otherwise, define the methods and synthesize.
    – muttley91
    Jun 10, 2013 at 16:16
  • Oh, in the .m file, should I declare the properties in the interface or implementation? Will this affect the automatic getter/setter behaviour?
    – muttley91
    Jun 10, 2013 at 16:22
  • You always declare properties in the @interface. So usually you do it in the .h file. This way this property will be available to external classes. If you don't want other classes to be able to see this property, you can declare it in the private category in the .m file. @implementation block can only contain @synthesize statements if they are necessary. Jun 11, 2013 at 9:53
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.

5
  • 2
    Thanks! I'll change the name and change the setter. I think that using @synthesize description = _description; might solve my problem (or, after I have to change the names anyway).
    – muttley91
    Jun 10, 2013 at 5:08
  • Yes, that is one way to force the compiler to synthesize the variable even if you override both.
    – borrrden
    Jun 10, 2013 at 5:09
  • A quick note that the "backing variables" referenced above are called ivars.
    – MattD
    Aug 19, 2014 at 20:37
  • @synthesize description = _description cannot fix any problem, because that's what is happening anyway if you don't synthesize at all.
    – gnasher729
    Jul 22, 2015 at 14:21
  • @gnasher729 If you override both the getter and setter, the ivar will not be automatically synthesized.
    – borrrden
    Jul 22, 2015 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.