Objective C Property Attributes

Just read a post about Objective C property attributes. I wanna make some extra notes on this topic:

1. “weak” vs “assign”

We generally use weak for Objective-C objects that we don’t need to retain, e.g.

@property (weak) IBOutlet UIButton aButton;

because its always good to nil an object if it has been deallocated.

On the other hand, we could use assign for C primitives, e.g.

@property BOOL isGood;

because C primitives has nothing to do with retain count and deallocation, simply assign the value is good enough.

Note that assign is the default property attribute, we can skip it for C primitives for cleaner code.

2. The default attribute of property vs variable

There are 2 kinds of default: property and variable

For property, the default attribute is “assign”

For variable (ivar or local variable), the default ownership qualifier is “__strong”

Don’t mix up “strong” in property and “__strong” in variable. They are different things (see below).

Also remember the default property attribute is “assign”, not “strong”

3. Implicit attribute for the default

The explicit version of

@property BOOL isGood;

should be:

@property (atomic, assign, readwrite) BOOL isGood

so skipping the attributes by using the default is fine for C primitives in most cases.

However, we better explicitly state the attributes for Objective-C objects. Because we seldom want “assign” for objects.

For ivar and local variable,

NSString *str;

is equivalent to

__strong NSString *str;

so if you set it as an ivar, the compiler will automatically retain it for you when you assign a value for it. Which behave differently from not using ARC.

4. Relationship between property attributes and ownership qualifiers

Here listed their relationships according to the official documentation

  • assign implies __unsafe_unretained ownership.
  • copy implies __strong ownership, as well as the usual behavior of copy semantics on the setter.
  • retain implies __strong ownership.
  • strong implies __strong ownership.
  • unsafe_unretained implies __unsafe_unretained ownership.
  • weak implies __weak ownership.

So for property attributes in ARC, practically we can assume assign = unsafe_unretained, and strong = retain.

Objective C Property Attributes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s