Strong vs copy, strongvs
I. Preface here, I will introduce the differences between strong and copy (the address of the object printed by % p) through an instance ViewController. h
#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property(nonatomic,strong) NSString *name;@endViewController.m - (void)viewDidLoad { [super viewDidLoad]; NSMutableString *myName = [NSMutableString stringWithString:@"zhangsan"]; self.name = myName; NSLog(@"myName's address:%p",myName); NSLog(@"self.name's address:%p",self.name);}
The running result is:
2015-07-21 16:38:50.911 Test[4369:2323491] myName's address:0x7967f4c02015-07-21 16:38:50.912 Test[4369:2323491] self.name's address:0x7967f4c0
When we change strong in. h to copy,
@property(nonatomic,copy) NSString *name;
The running result is:
2015-07-21 16:40:09.938 Test[4394:2330879] myName's address:0x78deb9b02015-07-21 16:40:09.938 Test[4394:2330879] self.name's address:0x78dea270
Comparison shows that when strong is used, the addresses of the myName object and self. name are the same, indicating that the two point to the same address space:
0x7967f4c0
When we change strong of name in. h to copy, the myName and self. name addresses are different, indicating that the two point to different address spaces. MyName is:
0x78deb9b0Self. name is:
0x78dea270This Note: When we use copy, it means that we have different buckets and store the same content. Therefore, if we modify myName, self. name will not be affected. Conclusion: here you should learn: 1. The difference between strong and copy 2. We can print the object address with % p.