標籤: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?
- #import
-
- @interface TestViewController : UIViewController
- {
- BOOL isShowing;
- }
-
- @property (nonatomic,weak)IBOutlet UIBarButtonItem *controlBtn;
- @property (nonatomic,weak)IBOutlet UIBarButtonItem *cameraBtn;
-
- -(IBAction)controlAction:(id)sender;
- @end
用右側按鈕,控制相機按鈕的隱藏和顯示:
[cpp] view plaincopyprint?
- #import "TestViewController.h"
-
- @interface TestViewController ()
-
- @end
-
- @implementation TestViewController
- @synthesize cameraBtn,controlBtn;
-
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- isShowing = YES;
- }
-
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- }
-
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
- }
-
- -(IBAction)controlAction:(id)sender
- {
- if (isShowing) {
- self.controlBtn.title = @"顯示相機";
- self.navigationItem.leftBarButtonItem = nil;
- isShowing = NO;
- }else {
- self.controlBtn.title = @"隱藏相機";
- self.navigationItem.leftBarButtonItem = cameraBtn;
- isShowing = YES;
- }
- }
- @end
實驗結果是,第一次隱藏了相機按鈕後,就再也顯示不出來了。原因很簡單,cameraBtn指向了空,我們丟失了cameraBtn的對象所有權。
解決問題的辦法有兩個:
1.不在xib或者storyboard上拖相機按鈕,而是用代碼建立,自己控制對象所有權
2.將 cameraBtn 定義為strong
我想強調的當然是第二種方法,當然了,改成strong後,相應的也需要配合ARC做下工作:
[cpp] view plaincopyprint?
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- self.cameraBtn = nil;
- }
順便提一下ARC其他屬性的規則:
-
strong:等同於"retain",屬性成為對象的擁有者
-
weak:屬性是 weak pointer,當對象釋放時會自動化佈建為 nil
-
unsafe_unretained:等同於之前的"assign",只有 iOS 4 才應該使用
-
copy:和之前的 copy 一樣,複製一個對象並建立 strong 關聯
-
assign:對象不能使用 assign,但原始類型(BOOL、int、float)仍然可以使用
最後一句,記憶規則,理解規則,善用規則。