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?
@synthesizedoes 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
@synthesizeand 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!!