2

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?

barndog
  • 6,975
  • 8
  • 53
  • 105
  • 1
    Backing variable synthesis is automatic, and therefore, @synthesize has effectively been deprecated. Really, you would only need it to declare a backing iVar with a different name than the auto-synth'd one (which is a terrible practice anyways). Remove em' all, and clean up your variable access. Nothing should change. – CodaFi Feb 02 '13 at 05:11
  • possible duplicate of [Automatically @synthesized properties in Xcode 4.4](http://stackoverflow.com/questions/11666008/automatically-synthesized-properties-in-xcode-4-4) – CodaFi Feb 02 '13 at 05:14
  • @CodaFi, what is backing variable ? – Arpit B Parekh Jan 19 '17 at 06:15
  • An `@property` declaration generates a set of accessors (getter [and setter assuming the property is not `readonly`]) and automatically allots itself storage in the instance with an implicit iVar declaration. When I speak of backing variables, I'm talking about that iVar. – CodaFi Jan 19 '17 at 06:17
  • ivar means instance variable ? that we declare insede interface block ? – Arpit B Parekh Jan 19 '17 at 06:21
  • i think yes,as marked answer says correctly as what you say. – Arpit B Parekh Jan 19 '17 at 06:25
  • @CodaFi, Now there is no need to write “synthesize” in your code , but only if you have instance variable (Inside interface block) and property variable, you have to write synthesize. Still do not understand _ funda. – Arpit B Parekh Jan 19 '17 at 06:57

4 Answers4

7

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.

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • 1
    Well that's awesome. Really good to know that. Upvote if you want ;) – barndog Feb 02 '13 at 05:14
  • @shadow sorry for some wrong info just checked the release notes and found it was present since 4.0. Hope you check these notes :) (edited my answer) – Inder Kumar Rathore Feb 02 '13 at 05:25
  • Ok awesome! Yeah I was wondering what the deal with because all the code I look at, things on github and whatnot, all use the synthesize declaration. Apple really needs to be more open about what they change and not just at WWDC :P – barndog Feb 02 '13 at 05:29
  • 1
    There is no difference they are just saving our time. And sometimes (most of the times with me) it happens that I forgot to write `@synthesize` and it's good for me that compiler generate it for me :) – Inder Kumar Rathore Feb 02 '13 at 05:33
2

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

Rob
  • 11,446
  • 7
  • 39
  • 57
2

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.

Aaron Wojnowski
  • 6,352
  • 5
  • 29
  • 46
2

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.

Kyle Fang
  • 1,139
  • 6
  • 14