Search code examples
iospropertiessynthesize

_iVar and @property?


I see someone writes codes like this

@interface SomeClass:<NSObject>{
       NSString * _iVar;

}

@property(nonatomic,retain)NSString *iVar;

and in implement file

@synthesize iVar = _iVar;

But I always like write codes like this:

@interface SomeClass:<NSObject>
@property(nonatomic,retain)NSString *iVar;

and in implement file

@synthesize iVar = _iVar;

They all works fine, and I can use an instance someClass to get iVar "someClass.ivar".I just know why?


Solution

  • You now don't even have to synthesize the iVars anymore. The latest Xcode will automatically create a backing instance variable for each property, with the property name prefixed with an underscore.