iOS engineer who likes clean, simple designs and sans-serif.

Loading

Property Cheatsheet in iOS

Even though I can’t count how many times I’ve used properties, I thought I would make a cheatsheet to cover the basic definitions. This article was the best one I could find to adequately explain all the concepts.

Term Definition

Property

Syntactical shortcut for declaring a class’s accessor (setter and getter) methods using a similarly named instance variable.

Accessor

An instance method that gets or sets the value of a property of an object.

Getter

Method that retrieves the value of an object’s property.

Setter

Method that changes the value of an object’s property.

Synthesize

Allows you to create a backing variable and override accessor methods. This is optional for Xcode 4.4+. This is mandatory if you override setters/getters.
Attribute Definition

getter=

Use a custom name for the getter method.

setter=

Use a custom name for the setter method.

readonly

Don’t synthesize a setter method.

nonatomic

Don’t guarantee the integrity of accessors in a multi-threaded environment. This is more efficient than the default atomic behavior.

strong

Create an owning relationship between the property and the assigned value. This is the default for object properties.

weak

Create a non-owning relationship between the property and the assigned value. Use this to prevent retain cycles.

copy

Create a copy of the assigned value instead of referencing the existing instance.

retain

Claims ownership of assigned values, pre-ARC version of strong.

unsafe_unretained

Similar to weak properties, but they don’t automatically set their value to nil if the referenced object is destroyed.

assign

Default behavior for primitive data types, and it used to be a way to implement weak references before iOS 5
Property:
@property (strong, nonatomic) NSString *title;

Synthesize (.m)
@synthesize title = _title;

Getter (.m)
- (NSString *)title;

Setter (.m)
- (void)setTitle:(NSString *)newTitle;

Leave a Comment