Objective C property in iOS application development gives you the core knowledge about how efficiently the memory management is done over Objects.
In this blog I have often used objects and i-vars. Let's first make ourself clear in 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 type of property attributes are exists in objective C. They are ..
- readonly
- readwrite
- atomic
- nonatomic
- retain
- 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 vs readwrite :
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 vs nonatomic :
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 vs copy :
retain- Is the way of referencing the same memory address without replicating it.
for instance .
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 : HELLOWORLDit'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
- mutablecopyConsider the 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