Created
January 9, 2014 04:13
-
-
Save keicoder/8329327 to your computer and use it in GitHub Desktop.
objective-c : assign and copy properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
assign and copy properties | |
Property with ’retain (or strong)’ attribute must be of object type | |
so, declare a property as assign if it has a primitive value | |
@property (nonatomic, assign) int someNumber; | |
You also use assign for structs, because they aren’t objects either | |
@property (nonatomic, assign) CLLocationCoordinate2D coordinate; | |
declare a property as copy, whatever object you try to give that property is copied first and the copy is stored instead of the original | |
This is a strong relationship, but with the newly copied object. | |
copy is used most often with strings and arrays to ensure that you get a unique object that cannot be changed out | |
e.g : | |
// on someObject: | |
@property (nonatomic, strong) NSString *text; ... | |
NSMutableString *m = @"Strawberry"; | |
someObject.text = m; // at this point, someObject.text is @"Strawberry" | |
[m appendString:@" and banana"](); // now both m and someObject.text are @"Strawberry and banana" | |
Because someObject’s text property is strong and not copy, both someObject.text and m refer to the same object | |
If m changes from @"Strawberry" to @"Strawberry and banana" then so will someObject.text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment