33

sorry I'm a newbie iOS developer, recently I've briefly heard that @synthesize is now @synthesize in a certain version of Xcode and that the compiler will auto synthesize and properties and assign _ to private variables.

I've tested this:

with out @synthesize, you can access properties and private field using

self.property_name = something;
_property_name = something; //(used in getter and setters)

with @synthesize property_name, you can access properties and private field using

self.property_name = something;
property_name = something;

My question is do we really need @synthesize anymore? or there is a bigger story I'm missing?

Rajan Balana
  • 3,775
  • 25
  • 42
Charlie Wu
  • 7,657
  • 5
  • 33
  • 40
  • 1
    the latest version of the compiler will put in the `@synthesize` for you if you leave it out in most cases. I just wrote about properties here: http://stackoverflow.com/questions/15493882/need-assistance-regarding-objective-c-properties-concept/15494036#15494036 – nielsbot Mar 25 '13 at 01:09
  • 1
    See http://stackoverflow.com/questions/11666008/automatically-synthesized-properties-in-xcode-4-4 – rmaddy Mar 25 '13 at 01:15
  • 2
    Depends on the phase of the moon, and, more to the point, which version of Xcode you're using. – Hot Licks Oct 21 '13 at 15:41

2 Answers2

55

No we don't need to do that as of Xcode 4.4, which added a feature called Default Synthesis Of Properties.

Simply put, it generates this automatically:

@synthesize name = _name;
James Chen
  • 10,794
  • 1
  • 41
  • 38
  • 18
    Caveat: If you override all of the auto-generated methods, this line will not be auto-generated (but you can still write it yourself to get the variable). Also, properties defined in protocols will not be auto-synthesized (instead you will get a compiler warning). Lastly, subclasses of `NSManagedObject` also have this behavior disabled because it is largely undesired. – borrrden Mar 25 '13 at 01:53
  • 4
    This answer is *too* simply put. For the details read this post: [Automatic Property Synthesis With Xcode 4.4](http://useyourloaf.com/blog/2012/08/01/property-synthesis-with-xcode-4-dot-4.html) – Basil Bourque Oct 20 '13 at 21:16
  • 3
    Another Caveat: If you have declared properties in a protocol, they will need synthesizing. – James Webster Oct 21 '13 at 15:50
  • so you only need/have to use it when you wanna rename a property declared in header. – Ilker Baltaci Sep 09 '15 at 07:07
-3

Unless you've been using it like I have, then you still need it, as far as I can tell:

in .h:

@property int32_t FwdID;

in .m:

@synthesize FwdID;

usage inside class:

FwdID = 0;

From what I can tell, if you want the default to work for you, you'll have to type _FwdID = 0; in the code, which just looks ugly to me.

So if you are like me (breaking all the standard coding conventions I assume), you'll still need to use synthesize.

eselk
  • 6,764
  • 7
  • 60
  • 93
  • You should access the properties via their getters and setters `[self setFwdID:0];`, otherwise use a real instance variable and not a property. – Maciej Swic Aug 12 '15 at 07:53