For beginners of the developers, for assign, retain, copy, strong, weak usage and meaning may not be very clear, I have studied this problem for a long time, write a blog, Qiao Qiao code, let's look at it!
Define a student class first:
#import <Foundation/Foundation.h> @interface student:nsobject@property (nonatomic, copy) NSString *name; @end
And then first the Assign statement from the MRC.
@property (nonatomic, assign) Student *stu1;
Next initialize a student object and typing the following code
-(void) viewdidload { [super viewdidload]; Additional setup after loading the view, typically from a nib. Student *stu = [[Student alloc] init]; Stu.name = @ "Zhang San"; SELF.STU1 = stu; NSLog (@ "%p%p", &stu, &_STU1); NSLog (@ "%p%p", STU,_STU1); Self.stu1.name = @ "John Doe"; NSLog (@ "stu.name =%@", stu.name); NSLog (@ "stu reference count =%ld", [Stu Retaincount]);}
Console output
(a) So I summarize, assign just make point to Stu stack memory on the pointer, that is Stu changed a name, replaced by STU1, is Stu and stu1 the role and meaning is the same, who did any change the corresponding point to the stack of memory content will also change, However, the reference count of the stack memory is still 1 no increase.
Next we look at retain, change the STU1 property to the following
@property (nonatomic, retain) Student *stu1;
Then rerun the program console output as:
(b) To sum up, retain is to make a pointer to the stack memory of one more, that is, the reference count plus 1, and the pointer Stu and STU1 for the memory of the stack is the same, that is, a door more than a key
Then look at the role of copy, also change the Stu property to copy, but if it is our definition of the object, then we have to implement the nscopying, so we can call copy, paste the code
Student.h
#import <Foundation/Foundation.h> @interface student:nsobject<nscopying> @property (nonatomic, copy) NSString *name; @end
Student.m
#import "Student.h" @implementation student-(ID) Copywithzone: (Nszone *) zone{ Student *stucopy = [[Self class] Allocwithzone:zone] init]; return stucopy;} @end
Viewdidload code unchanged, run program, console output
(c) It can be seen that the stu.name has not changed, and the reference count of Stu is not increased, why? Because copy is a complete copy of a stack of memory, so the copied object and the original object is two different addresses of the object, so there is no effect on Stu
Well, let's look at the difference and usage of strong and weak in arc mode.
First strong, declaring the attribute
@property (nonatomic, strong) Student *stu1;
Code in the Viewdidload
-(void) viewdidload { [super viewdidload]; Additional setup after loading the view, typically from a nib. Student *stu = [[Student alloc] init]; SELF.STU1 = stu; NSLog (@ "%p%p", &stu, &_STU1); NSLog (@ "%p%p", Stu, _STU1); Stu = nil; NSLog (@ "will Stu after Stagnation"); NSLog (@ "%p%p", &stu, &_STU1); NSLog (@ "%p%p", Stu, _STU1);}
Console output
(d) It can be seen that when the Stu is assigned to STU1, the two pointers to the stack memory is the same, that is, this is the same as retain, but when we will Stu stall, STU1 point to the stack memory does not become empty, this is the difference between strong and retain, When the stack memory as long as there are two or more than two pointers to the time, the pointer in which the null operation will not release the stack memory, that is, strong out of the object, for the stack memory control, and a little, arc under the default assignment is strong
Next, let's take a look at weak
The others are the same, change the Stu1 property to weak, run the program, console output
(v) It can be seen that when the Stu is assigned to STU1, the two pointers to the stack memory is the same, that is, this is the same as retain, but when we will Stu stall, STU1 point to the stack memory becomes empty, this is the difference between weak and strong, That is weak out of the object, there is no control over the stack memory
Here, why not nstring test, because nstring *str = @ "dddd", then str = @ "BBBB", is a new stack of memory to store "BBBB" is not on the original basis of the change.
Using string to do the retain test, you will find that no matter how retain, but change one of the variables, the other unchanged.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS development-assign, retain, copy, strong, weak differences