So I use the @property
key in my header file.
If I do that, I should use the @synthesize
key in my implementation, right? But I wonder, is there an actual reason I have to do that? I'm just wondering why isn't writing @property
in the header just about enough for the to code know my intentions (having the get/set methods automagically generated).
Sure, according to Why we have to synthesize? we write @synthesize
to generate the get/set methods. But my question is about why isn't @property
in the header just enough for this?
I ask because whenever I write @property
in my header, I immediately go to the implementation and write @synthesize
. So for me, the only reason @synthesize
is used is to complement the @property
keyword. Which seems rather redundant, and makes me assume that @synthesize
wouldn't exist if it wasn't because it has other uses. What are those other uses?
It's simply a matter of making connections or putting things together. We synthesize information naturally to help others see the connections between things. For example, when you report to a friend the things that several other friends have said about a song or movie, you are engaging in synthesis.
When you are entering a research conversation (by writing a paper, or giving a presentation), you need to identify the ideas in that research area to make connections between sources, and use your own voice to write about your interpretation of these ideas. This activity is called information synthesis.
@synthesize
does two things. It generates the getter/setter pair and it creates the iVar for the property.
Of these two things, I think the iVar creation is the key to when I use @synthesize
and when I don't. When create properties for members that are not internally stored as iVars, then (obviously) I don't use @synthesize
.
The upcoming auto synthesize feature is not going to be of much help. I always name my iVars with a leading '_', and so I will still need to explicitly synthesize them.
See @AndrewMadsen link: it looks like '_' prefix auto synthesize will generate the iVars.
W00t! Needless to say, I'm much more excited about auto synthesize now!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With