What is the difference between copy
and retain
for NSString
?
- (void)setString:(NSString*)newString
{
string = [newString copy];
}
In a general setting, retaining an object will increase its retain count by one. This will help keep the object in memory and prevent it from being blown away. What this means is that if you only hold a retained version of it, you share that copy with whomever passed it to you.
Copying an object, however you do it, should create another object with duplicate values. Think of this as a clone. You do NOT share the clone with whomever passed it to you.
When dealing with NSString
s in particular, you may not be able to assume that whoever is giving you an NSString
is truly giving you an NSString
. Someone could be handing you a subclass (NSMutableString
, in this case) which means that they could potentially modify the values under the covers. If your application depends on the value passed in, and someone changes it on you, you can run into trouble.
Retaining and copying are two different things, the first is conceptually call-by-reference while the second is call-by-value.
retain : It is done on the created object, and it just increase the reference count.
copy -- It creates a new object and when new object is created retain count will be 1.
Hope This Help for U...:)
Its an old post but here's my view on the question
Retain increases the retain count of an object by 1 and takes ownership of an object.
Whereas copy will copy the data present in the memory location and will assign it to the variable so in the case of copy you are first copying the data from a location assign it to the variable which increases the retain count.
Just remember that retain works on reference and copy works on value
if you use retain, it copy the pointer value from original one.retain also increment the reference count by one. but in case of copy, it duplicate the data referenced by the pointer and assign it to copy's instance variable.
The biggest difference is that if you use copy
, the object you are copying must implement the NSCopying
protocol (very easy to do). Not every object implements that, so you need to use care you know for sure what type you'll be operating against (or check for support of that protocol) when trying to call copy
.
The best rule of thumb to using copy
I can think of, is to always set NSString properties to "copy" instead of retain. That way you get more accurate readings from the Leaks instrument if you mess up and forget to release a string an object is holding onto. Other uses of copy need to be more carefully thought out.
copy: creates a new instance that's a copy of the receiver. It means that you'll have 2 different
retain: Increases the retainCount
of the receiver. An object is removed from memory - (with dealloc), when retainCount is 0.
Retaining an object means the retain count increases by one. This means the instance of the object will be kept in memory until it’s retain count drops to zero. The property will store a reference to this instance and will share the same instance with anyone else who retained it too. Copy means the object will be cloned with duplicate values. It is not shared with any one else.
retain attribute is specified such that it can retain the another memory i.e it can be made to point to another address also copy First copies the address and then retains it.
来源:https://stackoverflow.com/questions/2399490/what-is-the-difference-between-copy-and-retain