It's a little confusing from time to time. Retain, strong, copy the difference between the three, or to record the better, first look at the code:
Create a class that defines a property
#import <Foundation/Foundation.h> @interface Person:nsobject@property (nonatomic, retain) nsstring *strretain;@ Property (Nonatomic, Strong) NSString *strstrong; @property (nonatomic, copy) NSString *strcopy; @property (Nonatomic, Retain) nsmutablestring *strmutableretain, @property (nonatomic, strong) nsmutablestring *strmutablestrong; @property ( nonatomic, copy) nsmutablestring *strmutablecopy; @end
The code inside the Main method:
Person *per = [[Person alloc] init]; NSString *name = @ "Original XM"; [Per Setstrretain:name]; [Per Setstrstrong:name]; per.strcopy = name; nsmutablestring *name2 = [[Nsmutablestring alloc] initwithstring:@ "original XM2"]; Per.strmutableretain = name2; Per.strmutablestrong = name2; Per.strmutablecopy = name2; NSLog (@ "------print------before changing"); NSLog (@ "Per.strretain:%@,", Per.strretain); NSLog (@ "Per.strstrong:%@", Per.strstrong); NSLog (@ "per.strcopy:%@", per.strcopy); NSLog (@ "Per.strmutableretain:%@", Per.strmutableretain); NSLog (@ "Per.strmutablestrong:%@", Per.strmutablestrong); NSLog (@ "per.strmutablecopy:%@", per.strmutablecopy); Name = @ "New XM"; [Name2 appendstring:@ "New XM2"]; NSLog (@ "Print-------after-------change"); NSLog (@ "Per.strretain:%@", Per.strretain); NSLog (@ "Per.strstrong:%@", Per.strstrong); NSLog (@ "per.strcopy:%@", per.strcopy); NSLog (@ "Per.strmutableretain:%@", Per.strmutableretain); NSLog (@ "Per.strmutablesTrong:%@ ", Per.strmutablestrong); NSLog (@ "per.strmutablecopy:%@", per.strmutablecopy);
Printing results:
------Change before printing------Per.strretain: Original XM, Per.strstrong: Original xmper.strcopy: Original xmper.strmutableretain: Original Xm2per.strmutablestrong: Original xm2per.strmutablecopy: Original XM2-------changed after printing-------Per.strretain: Original xmper.strstrong: Original xmper.strcopy: Original Xmper.strmutableretain: Original XM2 new Xm2per.strmutablestrong: Original XM2 new xm2per.strmutablecopy: Original XM2
Conclusion:
1, for non-mutable objects: Retain, strong, copy of the role is the same, that is, when the reference value of the original object is changed, other references to the object's property values will not be affected, or maintain the original value;
2, for mutable objects: retain, strong and copy of the role of the difference, using the retain, strong decorated properties, when the reference value of the original object changed, the other reference to the object's property values will be changed together, and the value of the copy decorated property remains intact. The role of copy is mainly reflected here: let the value of the property will not change with the value of the original reference object;
3, retain and strong difference: the role is the same, just the difference between the wording. In the non-arc mechanism, it is modified with the Retain keyword, and after the arc mechanism, the strong keyword is used instead of the retain.
Retain, strong, and copy tests