Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Setters and Getters and Underscored Property Names

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?

like image 535
muttley91 Avatar asked Jun 10 '13 04:06

muttley91


People also ask

Can getters and setters have the same name?

If you want to use JavaScript getter/setter with the same name as the properties, e.g. to intercept certain setters to implement side effects, you can create a Proxy for your object.

What is property getters and setters?

What are Getters and Setters? Getters: These are the methods used in Object-Oriented Programming (OOPS) which helps to access the private attributes from a class. Setters: These are the methods used in OOPS feature which helps to set the value to private attributes in a class.

Are getters and setters bad?

Getter and setter methods (also known as accessors) are dangerous for the same reason that public fields are dangerous: They provide external access to implementation details. What if you need to change the accessed field's type? You also have to change the accessor's return type.

Should setters and getters be private?

The reason for declaring the getters and setters private is to make the corresponding part of the object's abstract state (i.e. the values) private. That's largely independent of the decision to use getters and setters or not to hide the implementation types, prevent direct access, etc.


2 Answers

@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];.

like image 77
FreeNickname Avatar answered Oct 18 '22 00:10

FreeNickname


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.

like image 32
borrrden Avatar answered Oct 18 '22 00:10

borrrden