iOS5 ARC,IBOutlets 應該定義strong還是weak

來源:互聯網
上載者:User

標籤:blog   http   color   使用   os   io   strong   for   

原帖:http://blog.csdn.net/yiyaaixuexi/article/details/7864974

 

寫這篇文章的緣由,是因為我泡在stackoverflow上翻文章,看到一個名為Should IBOutlets be strong or weak under ARC? 的文章很熱,而我對被採納為標準答案的回答也有一些話要補充,我想對於每一個初識ARC模式的人來說,都會有這個疑問,所以不妨我也來和大家探討一下。

 

有人問,在ARC下,IBOutlets到底應該定義成strong 還是 weak ?支援這個答案的人最多,答案僅是摘自官方文檔的一個片段:

 

From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should generally be weak, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong. Outlets that you create will therefore typically be weak by default, because:

  • Outlets that you create to, for example, subviews of a view controller’s view or a window controller’s window, are arbitrary references between objects that do not imply ownership.

  • The strong outlets are frequently specified by framework classes (for example, UIViewController’s view outlet, or NSWindowController’s window outlet).

    @property (weak) IBOutlet MyView *viewContainerSubview;@property (strong) IBOutlet MyOtherClass *topLevelObject;

大意是說,在 ARC 中,一般outlet屬性都推薦使用 weak,應該使用 strong 的 outlet 是 File‘s Owner串連到 nib 的頂層對象。 

什麼是 File‘s Owner串連到 nib 的頂層對象呢?說白話一點,就是自訂的view,不是直接作為main view裡面一個sub view直接顯示出來,而是需要通過執行個體化建立出來的。你自己執行個體化,當然需要strong了,不然誰還替你保留對象所有權呢?

 

以上的分析都沒有錯,但是總覺得少了點什麼。對於到底是weak 還是 strong,歸根結底,還是要刨到對對象所有權的問題上,但是不便於總結出淺顯易懂的規律性使用法則。於是,就會有一個又一個的特例打破文檔所總結的常規,不明白規則的根是什麼,還是會碰到麻煩的。

 

我來舉一個簡單的例子,建立一個程式入口指向navigation controller的工程,導覽列上拖2個按鈕:

右側按鈕用於控制相機按鈕的顯示與否,按照文檔的指示,我們在程式中定義這兩個按鈕應為weak屬性

 

[cpp] view plaincopyprint?
  1. #import   
  2.   
  3. @interface TestViewController : UIViewController  
  4. {  
  5.     BOOL isShowing;  
  6. }  
  7.   
  8. @property (nonatomic,weak)IBOutlet UIBarButtonItem *controlBtn;  
  9. @property (nonatomic,weak)IBOutlet UIBarButtonItem *cameraBtn;  
  10.   
  11. -(IBAction)controlAction:(id)sender;  
  12. @end  


 

用右側按鈕,控制相機按鈕的隱藏和顯示:

 

[cpp] view plaincopyprint?
  1. #import "TestViewController.h"  
  2.   
  3. @interface TestViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation TestViewController  
  8. @synthesize cameraBtn,controlBtn;  
  9.   
  10. - (void)viewDidLoad  
  11. {  
  12.     [super viewDidLoad];  
  13.     // Do any additional setup after loading the view, typically from a nib.  
  14.     isShowing = YES;  
  15. }  
  16.   
  17. - (void)viewDidUnload  
  18. {  
  19.     [super viewDidUnload];  
  20.     // Release any retained subviews of the main view.  
  21. }  
  22.   
  23. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  24. {  
  25.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  26. }  
  27.   
  28. -(IBAction)controlAction:(id)sender  
  29. {  
  30.     if (isShowing) {  
  31.         self.controlBtn.title = @"顯示相機";  
  32.         self.navigationItem.leftBarButtonItem = nil;  
  33.         isShowing = NO;  
  34.     }else {  
  35.         self.controlBtn.title = @"隱藏相機";  
  36.         self.navigationItem.leftBarButtonItem = cameraBtn;  
  37.         isShowing = YES;  
  38.     }  
  39. }  
  40. @end  


 

實驗結果是,第一次隱藏了相機按鈕後,就再也顯示不出來了。原因很簡單,cameraBtn指向了空,我們丟失了cameraBtn的對象所有權。

解決問題的辦法有兩個:

1.不在xib或者storyboard上拖相機按鈕,而是用代碼建立,自己控制對象所有權

2.將 cameraBtn 定義為strong

 

我想強調的當然是第二種方法,當然了,改成strong後,相應的也需要配合ARC做下工作:

 

[cpp] view plaincopyprint?
  1. - (void)viewDidUnload  
  2. {  
  3.     [super viewDidUnload];  
  4.     // Release any retained subviews of the main view.  
  5.     self.cameraBtn = nil;  
  6. }  

 

 

 

順便提一下ARC其他屬性的規則:

 

  • strong:等同於"retain",屬性成為對象的擁有者

  • weak:屬性是 weak pointer,當對象釋放時會自動化佈建為 nil

  • unsafe_unretained:等同於之前的"assign",只有 iOS 4 才應該使用

  • copy:和之前的 copy 一樣,複製一個對象並建立 strong 關聯

  • assign:對象不能使用 assign,但原始類型(BOOL、int、float)仍然可以使用 


 

最後一句,記憶規則,理解規則,善用規則。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.