• Home
  • Tutorials
  • xCode @property attributes (nonatomic, copy, strong, weak)
 
28
0
0

xCode @property attributes (nonatomic, copy, strong, weak)

Shekhar GuptaviaiOS Funda
June 10, 2013




Nonatomic
nonatomic is used for multi threading purposes. If we have set the nonatomic attribute at the time of declaration, then any other thread wanting access to that object can access it and give results in respect to multi-threading.
Copy
copy is required when the object is mutable. Use this if you need the value of the object as it is at this moment, and you don't want that value to reflect any changes made by other owners of the object. You will need to release the object when you are finished with it because you are retaining the copy.
Assign
Assign is somewhat the opposite to copy. When calling the getter of an assign property, it returns a reference to the actual data. Typically you use this attribute when you have a property of primitive type (float, int, BOOL...)
Retain
retain is required when the attribute is a pointer to an object. The setter generated by@synthesize will retain (aka add a retain count to) the object. You will need to release the object when you are finished with it. By using retain it will increase the retain count and occupy memory in autorelease pool.
Strong
strong is a replacement for the retain attribute, as part of Objective-C Automated Reference Counting (ARC). In non-ARC code it's just a synonym for retain.
This is a good website to learn about strong and weak for iOS 5.http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1
Weak
weak is similar to strong except that it won't increase the reference count by 1. It does not become an owner of that object but just holds a reference to it. If the object's reference count drops to 0, even though you may still be pointing to it here, it will be deallocated from memory.
The above link contain both Good information regarding Weak and Strong.


For example, If we have the following types declared - how should we be setting the properties?
@property (nonatomic, ??) NSMutableArray *myArray

@property (nonatomic, ??) NSString *myString

@property (nonatomic, ??) UIColor *myColor

@property (nonatomic, ??) int *myIn

@property (nonatomic, ??) BOOL *myBOOL


To reiterate, it does depend on context. In an non-ARC situation:
@property (nonatomic, copy) NSMutableArray *myArray

@property (nonatomic, copy) NSString *myString

@property (nonatomic, retain) UIColor *myColor

//Note the change to an int rather than a pointer to an int

@property (nonatomic, assign) int myInt

//Note the change to an int rather than a pointer to an int

@property (nonatomic, assign) BOOL myBOOL
The copy on myArray is to prevent modification by another "owner" of the object you set. In an ARC project, things change a bit:
@property (nonatomic, copy) NSMutableArray *myArray

@property (nonatomic, copy) NSString *myString

@property (nonatomic, strong) UIColor *myColor

//Note the change to an int rather than a pointer to an int

@property (nonatomic, assign) int myInt

//Note the change to an int rather than a pointer to an int

@property (nonatomic, assign) BOOL myBOOL










Discussion

-