In Objective-C, what does the code after an @synthesize mean?
-
20-04-2021 - |
Question
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
Solution
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.)
OTHER TIPS
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
, andage
should be synthesized and that the propertyage
is represented by the instance variableyearsOld
.
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.