Vinod
Vinod

Reputation: 93

KVC Compliance - Guidelines for declaring iVars

I have seen ivars that are backing properties declared in 2 general formats (in various books, on blogs, etc...) :

myProperty = _myProperty;
myProperty = myProperty_; 

Apple docs say the former one is needed for key value compliance

Which one is most appropriate ?

Upvotes: 1

Views: 344

Answers (2)

Jens Ayton
Jens Ayton

Reputation: 14558

The currently informal recommendation from Apple is to use an underscore prefix. There are indications that there will be syntactic conveniences for this in future.

Upvotes: 1

Chuck
Chuck

Reputation: 237080

If you're creating a property, the name of the underlying ivar doesn't make any difference to KVC — it will go through the accessor. It will only access the instance variable if both of the following are true:

  • It can't find any appropriate accessor for the key

  • The object's class returns YES for accessInstanceVariablesDirectly

Upvotes: 2

Related Questions