barndog
barndog

Reputation: 7163

Using @synthesize in ios Application

I was watching the wonderful Paul Haggerty in the iTunesU courses for iOS development (cause who doesn't need to refresh on the basics?) and he said something that I wasn't aware of:

"We will never access underbar ( the _ symbol) variables"

He then went on to talk about how when you use @property to declare your variables,@synthesize variable = _variable is code that's generated behind the scenes by the complier, as well as the setter and getter. Essentially that code never should appear in your app.

In all of my iOS apps I've written thus far, I always declare my variables using @property in my header file and @synthesize VARIABLE_NAME = _VARIABLE_NAME; Since watching the lecture, I'm now confused as to if I should be using @synthesize in my code at all.

Should I just use the property declaration? What difference does it make, if any, if I use the synthesize declaration in my code?

Since Mr. Haggerty doesn't use it, then why do I? (considering he's sort of an iOS demi-god). I very much feel like it's bad form to do what I've been doing.

Anyone care to clarify that issue?

Upvotes: 2

Views: 3109

Answers (4)

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

Xcode 4.0 Developer Preview 4 Release Notes. Adds default automatic synthesis of properties (iOS and 64-bit OS X). You don’t need the @synthesize directive in the implementation sections for the compiler to synthesize accessors for declared properties.

So

@synthesize ivar = _ivar;

is exactly same if you omit it.

Upvotes: 7

Kyle Fang
Kyle Fang

Reputation: 1199

with Xcode 4.5 or up. The IDE write the @synthesize statement for you.

The @synthesize statement is only write the setter and getter for you.

that, _variable_name is the instant variable.

The variable_name is only a method that returns the value of _variable_name by default.

when using the variable = <Statement or value>. its calling thesetVarable_Namemethod to set the_variable_name` by default.

Hope it helped.

Upvotes: 2

Aaron Wojnowski
Aaron Wojnowski

Reputation: 6480

Not including "@synthesize VARIABLE_NAME = _VARIABLE_NAME" will do the exact same thing as if you actually included it, since the compiler will automatically add that if you don't add anything.

Upvotes: 2

Rob
Rob

Reputation: 11733

There is no longer any need for synthesize. This was also covered in a WWDC session this year. Just use properties.

Upvotes: 2

Related Questions