4

In BNR iOS book the authors say to make this copy instead of strong:

@property (nonatomic, copy) NSString *itemName;

But I don't really understnad the purpose because in the main method I tried out:

BNRItem *calculator = [[BNRItem alloc] init];

        NSString *pickle = @"pickbarn";
        backpack.itemName = pickle;
        pickle = @"fuffle";

When I printed out backpack's name to the console it was picklebarn, so I don't really understand why itemName needs to be copied?

stumped
  • 3,235
  • 7
  • 43
  • 76
  • One addition to all the answers here -- you should not be worrying about performance in the case of copying `NSString` because it's implemented as just a retain. That's the reason you use `copy` for strings: it's cheap for `NSString` and safe for `NSMutableString`. – Alexei Sholik May 05 '13 at 21:09

4 Answers4

5

Because it's possible that a mutable string could be passed in.

(Also, IIRC, -copy on an immutable string just retains it under the hood.)

Wevah
  • 28,182
  • 7
  • 83
  • 72
  • Is it harmful that it's being retained twice? – stumped May 05 '13 at 21:08
  • 1
    @stumped - Nope. Each "owner" of the object retains it. That way, each owner can, when it'd done using the object, release its retain without having to coordinate with other "owners". When all retains are released then the object can be deleted and the space reclaimed. This is known as ["reference counting"](http://en.wikipedia.org/wiki/Reference_counting). – Hot Licks May 05 '13 at 21:12
4

As Wevah said; you don't know whether the string passed in is mutable or not. In the following example the backpack.itemName would end up being "fuffle" if you are not copying:

BNRItem *calculator = [[BNRItem alloc] init];
NSMutableString *pickle = [[NSMutableString alloc] initWithString:@"pickbarn"];
backpack.itemName = pickle;
[pickle setString:@"fuffle"];
Vegard
  • 4,352
  • 1
  • 27
  • 25
3

I have also studied the book you have mentioned and the program that you wrote above.

COPY is used when you are dealing with the objects that are mutable. You can use COPY to get the value of the object at a particular moment and at that particular moment, this value will be independent of the changes made by any other owners of the object.

Jessica
  • 1,508
  • 15
  • 25
1
 NSString *pickle = @"pickbarn";

pickle is a pointer. It points to an NSString in the memory.

 backpack.itemName = pickle;

now your backpack's itemName property points to the same NSString in the memory.

 pickle = @"fuffle";

pickle now points to a new NSString in the memory. But we have made no change to backpack's itemName property. It still points to @"pickbarn" in the memory.

This will work the same way when attribute your property strong or copy.

If you want to know more about the attributes (copy, strong) check out this thread.

Community
  • 1
  • 1
Cagdas Altinkaya
  • 1,710
  • 2
  • 20
  • 32
  • This doesn't answer the question. The question is why is the property defined with `copy` instead of `strong`. There is a difference but your answer doesn't address what the difference is. – rmaddy May 05 '13 at 21:10