2
@property (nonatomic, copy ) NSString *title;
@synthesize title = _title;

what does this '_title' mean?

Any help appreciated.Thanks in advance

kongkong
  • 333
  • 1
  • 3
  • 12
  • http://stackoverflow.com/questions/13297829/in-arc-what-happens-when-you-dont-synthesize – iPatel Aug 30 '13 at 06:21
  • Here are answers posted before. http://stackoverflow.com/questions/9086736/why-would-you-use-an-ivar http://stackoverflow.com/questions/822487/how-does-an-underscore-in-front-of-a-variable-in-a-cocoa-objective-c-class-work – GxocT Aug 30 '13 at 06:24

2 Answers2

3

_title is iVar. mean it is variable and title just is a property.You are synthesizing by assigning iVar to property.Because you need not write the methods like

- (NSString *)title {
    return _title;
}

- (void)settitke:(int)newValue {
    _title = newValue;
}

Those are getter and setters

when you do

title =_title.Those two above methods will abstract the setter and getter in one line.

NHS
  • 409
  • 2
  • 7
1

It means that the backing iVar for the property has a leading underscore.

So you can use property syntax to access it:

self.title = @"Some title";

or you access it directly, but using the underscore name

_title = @"Some title";

Although using direct access is discouraged except for initialisers and the dealloc method.

If you are using auto synthesis, the underscore is added by default.

The reason for this:

It makes it a lot clearer when you are using direct access to refer to an iVar rather than a local variable, or a property.

It also means that you can use obvious names for method parameters and not worry about them clashing; for example, if you were to write a method that referred to the property:

Abizern
  • 146,289
  • 39
  • 203
  • 257