Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an actual reason why do we have to use @synthesize?

Tags:

objective-c

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?

like image 296
Voldemort Avatar asked Jul 24 '12 15:07

Voldemort


People also ask

Why do we need synthesize?

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.

What does it mean to synthesize information and why is it important in research?

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.


1 Answers

@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!!

like image 184
Jeffery Thomas Avatar answered Sep 24 '22 02:09

Jeffery Thomas