Hi Forum,
I am not really understanding why it is good practice to use the copy semantic for use with properties whom classes have mutable and immutable types. I have read so many explanations, but for some reason I do not understand it. So I'm going to try to take a crack at it and if anyone can please tell me if I got it right.
Lets take the following
and in a method we have:
because both str and mutStr now both point to the same address lets say 0x01 due to the assignment operator, when we change the mutable string the immutable one uncontrollably will change. When we replace strong with copy when the setter is called the object is created but str will have address 0x02.
Now this wont be anything to worry about in a program that DOES NOT have any MUTABLE strings
did i get that right?
I am not really understanding why it is good practice to use the copy semantic for use with properties whom classes have mutable and immutable types. I have read so many explanations, but for some reason I do not understand it. So I'm going to try to take a crack at it and if anyone can please tell me if I got it right.
Lets take the following
Code:
@property (nonatomic, strong) NSString *str;
and in a method we have:
Code:
NSMutableString *mutStr = [NSMutableString stringWithString:@"hello"];
obj.str = mutStr;
[mutStr appendString:@"there"];
because both str and mutStr now both point to the same address lets say 0x01 due to the assignment operator, when we change the mutable string the immutable one uncontrollably will change. When we replace strong with copy when the setter is called the object is created but str will have address 0x02.
Now this wont be anything to worry about in a program that DOES NOT have any MUTABLE strings
did i get that right?