what is the difference among alloc ,copy and new?
What are the actual difference among alloc,copy and new and also 开发者_如何学编程what about the assign and nonatomic Property.
- alloc
alloc is a Class selector (that is for exemple called like)
[NSObject alloc]
It returns a non initialized pointer of type NSObject*.
- Init
To complete the initialization, you must call the proper designated initializer on the instance itself:
[[NSObject alloc] init]
Will return a usable NSObject* pointer.
- new
The new basically does an alloc -> init except it is called directly at class level:
NSObject* aObj = [NSObject new]
NSObject* aObj = [[NSObject alloc] init]
Are similar.
- nonatomic
A nonatomic property means that when the property will be written (ex during a set call) no lock will be added on the variable synthesized by this property (that means no overkill time consuming @synchronize).
So if your property will not be changed by different threads simultaneously, you can safely set it to nonatomic.
- copy
A copy property means that when you modify that property ex:
aObj.copyProperty = otherValue
The copyProperty variable will send the copyWithZone: signal to the otherValue object.
In other words, if your copyProperty is compliant with the NSCopying protocol it will eventually have the same properties as otherValue but will have its own address and retain count and therefore be located at a totally different part of the memory as the otherValue was.
Basically copyProperty will occupy as much memory space as otherValue.
- assign
Assigning a property means that when you do:
aObj.prop = aProperty
The variable synthesized by the property prop will directly be assigned to aProperty meaning they will share exact same address AND retain count.
No additional memory space is occupied when you use assign.
I hope this helps you. For further information, please read the Apple Memory Management Documentation
精彩评论