KVC Compliance - Guidelines for declaring iVars

354 Views Asked by At

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 ?

2

There are 2 best solutions below

0
On BEST ANSWER

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.

0
On

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