Tuesday, July 22, 2014

Property, and its instance variable, accessors with ARC

Before ARC, directly access instance variables will not affect the object's references—it did not extend the lifetime of the object. If the instance variable is associated with a property, then the property should be used to access the object, so that the reference can be properly managed based on property reference attribute (strong, weak, etc).

With ARC, instance variables are strong references by default—assigning an object to an instance variable directly will extend the lifetime of the object, unless the instance variable has __weak attribute. 


By default, a readwrite property will be backed by an instance variable, which will again be synthesized automatically by the compiler. Unless you specify otherwise, the synthesized instance variable has the same name as the property, but with an underscore prefix. For a property called firstName, for example, the synthesized instance variable will be called _firstName.

By default, the property's accessor methods are synthesized automatically by the compiler.
The synthesized methods follow specific naming conventions:
  • The method used to access the value (the getter method) has the same name as the property.
    The getter method for a property called firstName will also be called firstName.
  • The method used to set the value (the setter method) starts with the word “set” and then uses the capitalized property name.
    The setter method for a property called firstName will be called setFirstName:.

No comments:

Post a Comment