1, why NSString to use copy. Rather than strong/assign.
Case 1:
@interface Test ()
@property (nonatomic, strong) NSString *strongstring;
@property (nonatomic, copy) NSString *copyedstring;
@end
@implementation
-(void) test{
nsstring *string = [NSString stringwithformat:@ "abc"];
self.strongstring = string;
self.copyedstring = string;
NSLog (@ "Origin string:%p,%p", String, &string);
NSLog (@ "Strong string:%p,%p", _strongstring, &_strongstring);
NSLog (@ "copy string:%p,%p", _copyedstring, &_copyedstring);
@end
Results:
Origin String:0x7fe441592e20, 0x7fff57519a48
strong string:0x7fe441592e20, 0x7fe44159e1f8
copy string: 0X7FE441592E20, 0x7fe44159e200
Case 2:
@interface Test ()
@property (nonatomic, strong) NSString *strongstring;
@property (nonatomic, copy) NSString *copyedstring;
@end
@implementation
-(void) test{
nsmutablestring *string = [nsmutablestring stringwithformat:@ "abc"];
self.strongstring = string;
self.copyedstring = string;
NSLog (@ "Origin string:%p,%p", String, &string);
NSLog (@ "Strong string:%p,%p", _strongstring, &_strongstring);
NSLog (@ "copy string:%p,%p", _copyedstring, &_copyedstring);
Will something unexpected happen?
[string appendstring:@ "333"];
NSLog (@ "Origin string:%@", mstr);
NSLog (@ "Strong string:%@", _rstr);
NSLog (@ "Copy string:%@", _cstr);
}
@end
Results:
Origin String:0x7fe441592e20, 0x7fff57519a48
strong string:0x7fe441592e20, 0x7fe44159e1f8
copy string: 0X7FE441592E20, 0x7fe44159e200
origin string:abc333
strong string:abc333
copy STRING:ABC
Conclusion
1. When the source data is the NSString type, the copy/strong/assign result is the same, both copies of the object address are shallow copies
2. When the source data is a nsmutablestring type, using the Copy object address change is a new object, belonging to a deep copy. Strong/assign address Same, shallow copy
3. So when modifying a string, use copy as much as possible to avoid unexpected problems.
Finally, I recommend a simple book that I feel summed up very well: https://www.jianshu.com/p/2e1b3f54b4f3