Question

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?

Était-ce utile?

La 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.

Autres conseils

In Objective-C when you are using anything via "." (dot syntax), it is accessing property. You have declared and synthesize a property with name iVar, so you can access it with someClass.iVar.

When you are declaring a property, a variable is automatically created with it. So, even if you do not create a variable, it will be there.

To avoid ambiguity, people are creating properties and iVar with different names. So, they can identify if the variable is being used or a property is being used. In that case people let the compiler know that a particular variable is associated with a property by low way:

@synthesize iVar = _iVar;

If you want access variable directly outside the class instead of property, you should use "->" (arrow syntax).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top