Strong vs copy, strongvs

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.