30

What is the difference between copy and retain for NSString?

- (void)setString:(NSString*)newString
{
    string = [newString copy];
}
Lorenzo B
  • 33,006
  • 23
  • 110
  • 185
senthilMuthu
  • 9,258
  • 18
  • 74
  • 139

9 Answers9

46

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 NSStrings 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.

Lorenzo B
  • 33,006
  • 23
  • 110
  • 185
Malaxeur
  • 5,928
  • 1
  • 33
  • 34
  • 16
    It should be mentioned that `copy` is equivalent to `retain` for most of the `Foundation` classes that aren't mutable. – rpetrich Mar 08 '10 at 08:44
  • 3
    Might I suggest that "you **share** that *instance* with whomever passed it to you" is clearer than "you **share** that *copy* with whomever passed it to you"? (Seeing as we're talking about copying already) –  May 02 '12 at 11:15
24

Retaining and copying are two different things, the first is conceptually call-by-reference while the second is call-by-value.

BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
iTayb
  • 10,984
  • 21
  • 71
  • 128
11

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...:)

iPhone NewBIe
  • 397
  • 1
  • 6
  • 16
6

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

NSDumb
  • 1,370
  • 12
  • 17
5

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.

Kester
  • 79
  • 1
  • 3
3

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.

Kendall Helmstetter Gelner
  • 73,251
  • 26
  • 123
  • 148
1

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.

ocodo
  • 27,324
  • 15
  • 97
  • 113
mxg
  • 19,703
  • 12
  • 56
  • 77
  • 1
    I think you mean "dealloc'ed" (a little awkward to verbify that word), not "deadlocked" – Brian Jul 02 '10 at 16:24
1

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.

0

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.