Objective C Property Attributes

Objective C property in iOS application development gives you the core knowledge about how efficiently the memory management is done over Objects.

In this article I have often used objects and i-vars. Let’s first understand what is objects and i-vars. In the @interface class whatever variables(primitive variables), objects(objective c objects NSString, NSArray, NSDictionary… etc) we used is referred as instance variables (i-vars). So, clearly objects is the subset of i-vars. And the non-object i-vars are primitive data type variables.

Objective C property attributes are used to define the functionality of the i-var that has been created inside the @interface class.

There are 6 primary property attributes exist in objective C.

  1. readonly
  2. readwrite
  3. atomic
  4. nonatomic
  5. retain
  6. copy

These are the basic objective C property’s attributes. Each of the above shares contradictory functionalities with other one.
ie., The contradictory pairs are … readonly — readwrite, atomic — nonatomic, retain — copy.

Note: You can’t use the contradictory pair together in setting property for an i-var or object.

readonly- Used to create readonly objects or instance variables(i-vars). The objects can be NSString,NSArray,NSDictionary…etc.
note: If we try to change the value of the readonly i-var, which costs an error/ system crash.

readwrite — Is straight opposite to the readonly. It allows to change the i-var at any time. That’s why readwrite is set as default.(ie., you no need to specify as readwrite, by default the compiler would take this as readwrite.)

atomic- Is pretty interesting property, in a multi threading concept, if you’re trying to apply accessors(getters), mutators(setters) for a same i-var / object by multiple threads. It allows the threads one at a time, till the current thread finishes it’s operation, all other threads are set as to wait. So the updation over the i-var / object is reflected to all other threads. Though it’s time consuming, but the security and reliability over the i-var / object value is really high. Since it follows the synchronous approach.
note: atomic also default attribute of property.

nonatomic — Is straight opposite to the atomic property. Unlike atomic, nonatomic follows asynchronous approach. ie., In multi threading if you’re trying to apply accessors and mutators for the same i-var / object by multiple threads, It costs unreliable data. Since it follows asynchronous approach over the i-var / object. This is not recommended for the multi threading applications in ios.

retain- Is the way of referencing the same memory address without replicating it. for example…

NSMutableString *firstString = [NSMutableString stringWithString:@"HELLO"];
NSMutableString *secondString = [firstString retain];

Now, the firstString, secondString both refers the memory address of string “HELLO”. If you’re trying to modify the HELLO via either of the objects(firstString, secondString), would be resulted to other object.
ie., modify the HELLO via the firstString

[firstString appendString:@"WORLD"];
NSLog(@"firstString : %@",firstString);
NSLog(@"secondString : %@",secondString);

results as,

firstString : HELLOWORLD
secondString : HELLOWORLD

it’s vice versa for the secondString also.
note: The ios 5 or later version retain is replaced by strong.

copy- Is straight opposite to the retain property. Unlike retain, copy creates a replica of the i-var / object.
And it has no link with the original object / i-var from it is copied. copy has two classifications in it. They are…

- copy- mutablecopy

Consider below example…

NSMutableString *firstString = [NSMutableString stringWithString:@"HELLO"];
NSMutableString *secondString = [firstString copy];

Here, copy is used, where it creates duplicate HELLO string in another (NSMutableString *) object secondString. If you’re trying to modify the object / i-var that is copied ( just copied), would costs system crash.

[secondString appendString:@"WORLD"];
NSLog(@" secondString : %@", secondString);

secondString is just copied object. If you’re trying to appendString with it, results system crash. So, copy creates an immutable replica of the object / i-var.

mutablecopy- Unlike copy, mutablecopy creates a mutable replica of the object / i-var.

NSMutableString *firstString = [NSMutableString stringWithString:@"HELLO"];
NSMutableString *secondString = [firstString mutablecopy];
[secondString appendString:@"WORLD"];
NSLog(@" secondString : %@", secondString);

results as,

secondString : HELLOWORLD

Apart from the above basic most used property attributes, there exists other attributes also… they are,

- assign- weak

assign- Is used when a setter assigns non-object i-vars (int, float, char.. etc).
So, anything that is not object can be used by assign. Since non-objects need not to be retained, copied… etc. assign refers simple assignment by default, to anything but which is not an object.

weak — Is a reference that you do not retain. — We generally use weak for IBOutlets (UIViewController’s Children). This works because the child object only needs to exist as long as the parent object does. A weak reference is a reference that does not protect the referenced object from collection by a garbage collector. weak is essentially assign, a unretained property. Except when the object is deallocated the weak pointer is automatically set to nil.

iOS Engineer