Need explanation on @property/@synthesize in iOS

29,193

Solution 1

There is no correct way. There is only your preferred style.

The lastest compilers do an implicit synthesise on the property declaration.

@synthesize list = _list; .

Nothing is ever written in your code. It just happens.

However that doesnt stop you doing this explicitly.

@synthesize list = somethingelse;

So when you ask for a pointer to list via the accessor (self.list) you will get a pointer to somethingelse

In most cases NSMutableArray *thing = self.list is equivalent to NSMutableArray *thing = somethingelse

And just because Apple uses a style doesn't mean that you have to do it. Each company usually has their own coding style.

The main problem with using @synthesize list; is that it poses the risk that you can write either

self.list = thing or list = thing .

The former uses the sythesised setList: accessor while the latter doesn't and put the risk of related bugs in your code , though its not as bad with ARC as you dont get leaks happening for strong properties.

What ever style you use, keep it consistent and be aware of the effects of using an ivar directly list = thing as compared to using its accessor self.list = thing

Solution 2

This is a language feature that has had its usage evolve rapidly over the past few years, which explains the various forms. With the most recent tools, you can choose to ignore @synthesize and have things work reasonably.

The default behavior in that case produces the same effect as @synthesize list = _list;.

Share:
29,193
HpTerm
Author by

HpTerm

Updated on April 03, 2020

Comments

  • HpTerm
    HpTerm about 4 years

    Not a complete noob I am quite new to iOS programming and to Ojbective-C. I mainly come from a background of C (DSP, Microcontrollers), Delphi XE2/Pascal , Visual Basic and Java (desktop and Android apps).

    I mainly learned Cocoa with the book "Beginning iOS 5 Development", from Apress.

    Recently I watched videos of the WWDC 2012 and went through some of their sample code, and I must say that I am confused of what is the proper way of writing my apps and more specifically with the @property/@synthesize words.

    In the book most (not to say all) of the sample code uses to define a property as for example

    ViewController.h
    @property (strong, nonatomic) NSMutableArray *list;
    
    ViewController.m
    @synthesize list;
    

    then all the code access synthesize list with

    self.list
    

    or even simply

    list
    

    Now in every WWDC code sample I read I see that programmers define a property but then, in the .m file they do things like

    @synthesize list = _list;
    

    and access sometimes

    self.list
    

    or

    _list
    

    I am confused. What is the correct practice ? As Apple programmers all use underscore I think I should do it that way but why the book did not ? Is there a difference between list and _list ? And more over, as I am in the same object why sometime use self.list and sometimes list/_list.

    Sometimes they don't use @synthesize, I assume it's when they want to re-write their own accessors and mutators (which is never my case up to now).

    I have read here and there on the web but nothing was clear enough to set my mind right, so I count on StackOverflow to bring light here.

    Last but not least I prefer and answer based on current iOS 6 best practice/programming technique. Useless to tell me how to do it correctly in older iOS.

  • HpTerm
    HpTerm over 11 years
    ok that's much clearer. Inside of the same object is that a real problem of accessing directly the ivar ? I mean I think in java getters and setters are used to access the variable outside of the class but internally of the class the variable is called directly. Isn't that the same here ? In iOS a mutable array is usually used to hold table view data and in my code i use to write "list =". Is that "wrong" and I shall use self.list ?
  • Warren Burton
    Warren Burton over 11 years
    its not wrong , just be aware that when you use "list = foo" you are NOT calling "[self setList:foo]" . ivars are strong by default so it works under ARC.
  • HpTerm
    HpTerm over 11 years
    And using the _list generated automatically by recent compiler will ensure that accessor/mutators are used. Right ? And so the only reason to use @synthesize nowadays is to redefine custom accessors/mutators ?
  • Warren Burton
    Warren Burton over 11 years
    no you still get a direct pointer that way. Theres nothing special about putting an underscore at the front of a var. Its a style only. the only way you use the accessors is if you call "self.something" or its equivalent get/set forms setSomething:foo / -(id)something;
  • HpTerm
    HpTerm over 11 years
    As you wrote "In most cases NSMutableArray *thing = self.list is equivalent to NSMutableArray *thing = somethingelse" I thought It was using the accessors but what you mean is that usually for a list it's not much different. So I don't get the point, why does apple use _list ? If it's still a ivar it seems useless to me. Moreover if it doesn't use the getters/setters why does apple use the underscore for the ivar and not simply "list". There must be a reason behind that. Do you have any idea ?