What are the default attributes for Objective-C properties?

Objective-C property attributes

[Objective-C property]

In Objective-C, when you write @property, after you write a list of attributes

default attributes:

  • pointer: strong, atomic, readwrite

  • primitive: assign, atomic, readwrite

    @property (atomic, readwrite, strong) NSString *someString;
    @property (atomic, readwrite, assign) int someInt;
    

Atomicity is only one part of thread-safe - Atomic Operations[About]

  • atomic (default) - which guarantee that you will not get some junk memory, always it will include some value.

  • nonatomic - you are able to get a garbage data in multi-thread envirompment, but it is a little bit faster for lack of additional logic

Access - modify access

  • readwrite (default) - getter and setter are generated and they can be used

  • readonly - only getter is generated. You are not able to set some value

Storage - memory management for backed instance variables (backing iVar)[About] which are generated automatically

  • strong (default for pointer) - object can not be deallocated while strong backed iVar has a reference to it

  • retain historically was created before strong with the same idea. the difference is that retain should be used when ARC is disabled

  • assign (default for primitive) - out of reference counting

  • weak - this reference is not get into account (+1) as strong do. That is why when all others strong references go out of our object, our reference will be point on nil. It is used to fight with Retain cycle and delegate pattern is a good example[About]

  • unsafe_unretained - similar to weak but when no one reference to object, our iVar will be point on some garbage(dangling pointer) and behaviour is not expected. It can have a little bit better performance than weak

  • copy - a new copy of object is assigned to iVar (NSCopying protocol) when you pass some object into the setter of our property

[Swift weak vs unowned]


it is equal as

@property (atomic, readwrite, assign) float value;

The default/implicit values are atomic, readwrite, and assign.

atomic

This means that the value is read/written atomically. Contrary to the somewhat popular misconception, atomicity does not equate to thread safety. In simple terms, it guarantees that the value you read or write will be read or written in whole (when the accessors are used). Even when you use accessors all the time, it's not strictly thread safe.

readwrite

The property is given a setter and a getter.

assign

This default is usually seen used for POD (Plain-Old-Data) and builtin types (e.g. int).

For NSObject types, you will favor holding a strong reference. In the majority of cases, you will declare the property copy, strong, or retain. assign performs no reference count operations. See also: http://clang.llvm.org/docs/AutomaticReferenceCounting.html#property-declarations

strong

The property may be implicitly strong under ARC in some cases:

A property of retainable object pointer type which is synthesized without a source of ownership has the ownership of its associated instance variable, if it already exists; otherwise, [beginning Apple 3.1, LLVM 3.1] its ownership is implicitly strong. Prior to this revision, it was ill-formed to synthesize such a property.