strong vs copy,strongvs

來源:互聯網
上載者:User

strong vs copy,strongvs

一、前言          在這裡,我通過執行個體去介紹strong和copy的區別(%p列印出來對象的地址)     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);} 
        運行結果是:
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 
         而當我們把.h中的strong改成copy時,
@property(nonatomic,copy) NSString *name;
        運行結果是:
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
       對比可以發現:        當我們使用strong時,myName對象和self.name的地址相同,說明兩者指向的是同一個地址空間: 0x7967f4c0         而當我們把.h中name的strong改成copy時,myName和self.name地址不同,說明兩者指向的是不同的地址空間。          myName是: 0x78deb9b0            self.name是: 0x78dea270     這說明:當我們使用copy的時候表示擁有不同的儲存空間,存放的是相同的內容。因此如果我們對myName進行修改,self.name是不會受到影響的。 總結:  這裡你應該學到:    1、strong和copy的區別    2、我們可以用%p列印出對象的地址。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.