3

I've been playing with iOS for several months, always use @synthesize to "make setter and getter" of @property. But in some tutorials, I often see @synthesize param = _param, I don't quite get its meaning.

For example: in .h file

@property (nonatomic, retain) NSString *param1;
@property (nonatomic, retain) NSString *param2;

in .m file

@synthesize param1; //this is what I like to do
@synthesize param2 = _param2; // this is what 'experienced' programmer does

From my habit, I can use self.param1, or just param1 to get this instance, is there any difference by the way?

From others, they seem like to use _param2 instead of other approach. I know it has something to do with getter/setter, but I still not quite clear.

Could someone explain their difference and pros/cons?

Thanks a lot.

JimZ
  • 1,182
  • 1
  • 13
  • 30
  • 1
    `@synthesize` is no longer required. "Experienced" programmers don't have `@synthesize` in their new code at all ;-) – Sergey Kalinichenko Sep 20 '13 at 11:01
  • This will be good read. [Automatic Property Synthesis With Xcode](http://useyourloaf.com/blog/2012/08/01/property-synthesis-with-xcode-4-dot-4.html) – Amar Sep 20 '13 at 11:04
  • @dasblinkenlight, are you saying (at)synthesize is deprecated? Or just no longer needed? Does Xcode automatically generate getter/setter if I just make (at)property in .h file? – JimZ Sep 20 '13 at 11:04
  • 1
    @JimZ `@synthesize` is not deprecated, but it became optional several months ago. When the compiler sees a `@property` without a corresponding `@synthesize`, it synthesizes the missing methods as needed (i.e. it wouldn't synthesize a method the implementation of which you provide; it would also not synthesize setters for read-only properties). – Sergey Kalinichenko Sep 20 '13 at 11:10
  • Ultimately, "_" is just another character that can be legally used in C/Objective-C names. It's somewhat traditional to name the instance variables "behind" properties with a leading underscore, but (assuming assorted defaults are overridden) it has no effect on program execution. – Hot Licks Sep 20 '13 at 12:12

2 Answers2

8

When you use

@synthesize param2 = _param2;

means that you are using a different name to access the instance variable directly.

(In new XCode versions, if you dont specify the synthesize yourself XCode writes one for you, same as that one)

If you use:

_param2

You are accessing the instance variable directly.

If you use:

self.param2

You are accessing the variable trough the setter/getter, and those setter/getter are defined using the properties you set.

As a rule of thumb, you want to access you ivar directly in the init methods, and in the rest of the class you use self.

If you want to get more info on this, just follow this link:

Encapsulating data in Objective-C

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
2

@synthesize param2 = _param2; Uses another name for the underlying instance variable. The pro is that you don't access the ivar by mistake as easily.

These days you get synthesize automatically and it uses the second variant so just leave out the @synthsize all together and use self.param and _param.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205