Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying an immutable string?

Tags:

objective-c

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?

like image 989
stumped Avatar asked Dec 11 '22 15:12

stumped


1 Answers

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

like image 164
Wevah Avatar answered Jan 17 '23 18:01

Wevah