IOS-strong and copy, iOS-strongcopy
Strong and copy are commonly used modifiers. When to use strong and copy, the previous Code will be used first (the following code is directly written in ViewController );
Define two arrays first
///strong@property (nonatomic,strong) NSArray *arraystrong;///copy@property (nonatomic,copy) NSArray *arraycopy;
ViewDidLoad Method
-(Void) viewDidLoad {[super viewDidLoad]; NSMutableArray * arrayMut = [NSMutableArray array]; // Add a string [arrayMut addObject: @ "xiaogui1"]; self. arraystrong = arrayMut; self. arraycopy = arrayMut; // Add another string [arrayMut addObject: @ "xiaogui2"]; NSLog (@ "arraystrong = % @ \ narraycopy % @", self. arraystrong, self. arraycopy );}
Print results
We can see that arraystrong has changed with the change of arrayMut, while arraycopy has not changed with the change of arrayMut. The reason is that when strong is used for modification, no new space is created during the assignment, the Pointer Points to the memory space of the same pointer. When using copy, a new memory space is created, instead of pointing to the same memory as arrayMut. However, there is another problem:
When I change self to underline _ to reference a variable:
-(Void) viewDidLoad {[super viewDidLoad]; NSMutableArray * arrayMut = [NSMutableArray array]; // Add a string [arrayMut addObject: @ "xiaogui1"]; /// change self to _ arraystrong = arrayMut; _ arraycopy = arrayMut; // Add another string [arrayMut addObject: @ "xiaogui2"]; NSLog (@ "arraystrong =%@ \ narraycopy % @", self. arraystrong, self. arraycopy );}
Print result:
This is because the get set method is called when self is referenced, that is, the difference between strong and copy is realized, and the direct use of _ does not execute the get set method, therefore, the values of the two arrays are the same. Therefore, it is better to use self in projects. In my opinion, please note that there are errors.