In Objective-C, what does the code after an @synthesize mean?

2.3k views Asked by At

Sorry for the bad title, I wasn't sure what else to call this question. Anyways, suppose I have the following:

@synthesize display = _display

My question is what is the difference between display and _display? What does each refer to?

To clarify, I know what @synthesize does (it creates getters and setters) but I'm not sure exactly what the difference what display and _display refer to.

I'm proficient at java, so whenever possible relate your answer to it.

Thanks

3

There are 3 answers

5
AudioBubble On BEST ANSWER

display is the name of the property. It also affects what the getters and setters will be called.

_display is the underlying field in the object which the property will access. Some people like to leave it the same as the property -- this behavior occurs by default, so it's easy; some people like to prefix their fields with underscores. (Some crazy people also like to use completely different names, but they're crazy.)

0
Georg Fritzsche On

From Declared Properties - Property Implementation Directives:

You can use the form property=ivar to indicate that a particular instance variable should be used for the property, for example:

@synthesize firstName, lastName, age=yearsOld;

This specifies that the accessor methods for firstName, lastName, and age should be synthesized and that the property age is represented by the instance variable yearsOld.

0
madmik3 On

this is saying that the display getter and setter provide access to the _display member variable.

you can use this to rename your properties externally.