19

While I was playing and figure out how things work in https://github.com/enormego/EGOTableViewPullRefresh I found mysterious of @property and @synthesize. Here is the code I mentioned

EGORefreshTableHeaderView.h

@interface EGORefreshTableHeaderView : UIView {
    id _delegate;
    EGOPullRefreshState _state;

    UILabel *_lastUpdatedLabel;
    UILabel *_statusLabel;
    CALayer *_arrowImage;
    UIActivityIndicatorView *_activityView;
}

@property(nonatomic,assign) id <EGORefreshTableHeaderDelegate> delegate;

EGORefreshTableHeaderView.m

@synthesize delegate=_delegate;

I have read this http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html and from what I understand is it create new name for _delegate which is delegate. (Am I right with this understanding ?)

But I still doesn't understand why they have to make thing complicated with those @synthesize = directive.

Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
sarunw
  • 8,036
  • 11
  • 48
  • 84
  • 7
    [1](http://stackoverflow.com/questions/3802851) [2](http://stackoverflow.com/questions/382051) [3](http://stackoverflow.com/questions/3266467) [4](http://stackoverflow.com/questions/5170631) [5](http://stackoverflow.com/questions/3277209) [6](http://stackoverflow.com/questions/719788) [&c.](http://stackoverflow.com/search?page=2&tab=relevance&q=objc%20property%20synthesize%20variable%20name) – jscs Apr 28 '11 at 07:40
  • possible duplicate of [Objective-C synthesize property name overriding](http://stackoverflow.com/questions/3802851/objective-c-synthesize-property-name-overriding) – Caleb Apr 28 '11 at 18:15
  • possible duplicate of [What does @synthesize window=_window do?](http://stackoverflow.com/questions/5170631/what-does-synthesize-window-window-do) – bzlm Nov 20 '11 at 17:38

2 Answers2

30

How complicated is it, really? It's just a bit of syntax that lets you specify the ivar that you want to use to back the property for which you're telling the compiler to create accessors. If they didn't provide this or something equivalent, then you'd always have to have your property names match your ivar names, and there are reasons that you might not want that.

If you don't need to name your ivars differently, then you don't have to bother with specifying the ivar name. In fact, you don't have to create ivars at all for your properties... if you don't, the compiler will create them for you.

Update: As of the middle of 2013, LLVM defaults to synthesizing accessors for properties, so in most cases you no longer need to specify @synthesize at all. The one case where you would still use it is when you want to back the property with a different instance variable than the one that the compiler would generate for you. Also, the default name for the ivar that backs a property will be the property name prefixed with an underscore. So, the code in the OP's example could be simplified by deleting the lines:

id _delegate;

and:

@synthesize delegate=_delegate;

I've removed my previous advice against using an underscore prefix since it clearly disagreed with the current fashion and default behavior of the compiler. As far as I know, it's still poor form to use an underscore prefix for your method names, however.

Also, it has come to my attention that at least one person interpreted the first line of my response, "How complicated is it, really?" as condescending. I hope that was only one person's impression -- I definitely didn't intend any condescension, but was only trying to frame my response around the OP's assertion that the @synthesize xxx=_xxx; directive makes things complicated. There's a lot to absorb when you're starting out; hopefully the new "synthesize by default" behavior will reduce the burden for newcomers.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 6
    I'm very new in obj-c and this isn't my code as I mentioned, so this is complicated for me. So what happening here is just a two name referencing same obj right ? @synthesis newName = ivar won't create new another ivar, but referenced the old one right ? – sarunw Apr 28 '11 at 07:10
  • 1
    Apple’s emphasis is on not having method names with an underscore prefix. –  Apr 28 '11 at 07:22
  • 9
    @art With `@synthesize delegate = _delegate`, there’s only one instance variable named `_delegate` and two accessor methods named `-delegate` and `-setDelegate:`. If it were `@synthesize delegate` instead, there’d be two instance variables, `_delegate` and `delegate`, plus two accessor methods named `-delegate` and `-setDelegate:`. –  Apr 28 '11 at 07:25
  • 1
    Leading underscores for ivars are perfectly acceptable and in common use. http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml#Trailing_vs_Leading_Underscores – John Starr Dewar Dec 20 '12 at 17:08
  • 5
    Since I was the one who interpreted the first line as condescending, I thought I might as well comment. I'd just spent quite some time trying to figure out why @synthesize wasn't doing what I expected, and this was one of the first google search results. After struggling with the directive for a while, reading "How complicated is it?" did sound condescending, but I appreciate the follow up. Thanks for the detailed answer. – Achal Dave Jul 26 '13 at 00:05
17

You are right, using

@synthesize foobar=_foobar;

is a bit pointless in most cases , but at an abstract level it does allow you to return the value of some other variable entirely. As in ...

@synthesize foobar=fluffybunny;

Lets you get or set the value of fluffybunny each time you use the accessor .foobar

However in terms of the@synthesize complexity , would you rather write

-(void)setFoobar:(id)aobject {
    [self willSetValueForKey:"foobar"];
    id old = foobar;
    foobar = [aobject retain];
    [old release];
    [self didSetValueForKey:"foobar"];
}

-(id)foobar {
    [self willAccessValueForKey:"foobar"];
    id obj = [self primitiveValueForKey:@"foobar"];
    [self didAccessValueForKey:"foobar"];    
    return obj;
}

Or

@synthesize foobar;

Thats not particularly well written as ive forgotten how to do them well but the @synthesize directive stops you having to write accessors so many times. It one one of the things that sucked heavily about Obj-C 1.0.

Free code , dont knock it.

Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
Warren Burton
  • 17,451
  • 3
  • 53
  • 73
  • 7
    the latest compiler in Xcode 4.4.1 forward means you dont even have to use synthesize xyz anymore . You get an implicit declaration of that when you declare @property xyz , gives you synthesize xyz = _xyz for free. – Warren Burton Sep 10 '12 at 15:08
  • 2
    @WarrenBurton correct, but if you custom implement both the getter and the setter of of `xyz` then you still have to use `@synthasize xyz=_xyz` – Rukshan Oct 29 '13 at 11:55