久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

IBOutlet 和其他的弱或強(qiáng)

weak or strong for IBOutlet and other(IBOutlet 和其他的弱或強(qiáng))
本文介紹了IBOutlet 和其他的弱或強(qiáng)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我已將我的項(xiàng)目切換到 ARC,但我不明白是否必須對(duì) IBOutlets 使用 strongweak.Xcode 這樣做:在界面生成器中,例如,如果創(chuàng)建一個(gè) UILabel 并且我將它與助手編輯器連接到我的 ViewController,它會(huì)創(chuàng)建這個(gè):

@property (nonatomic, strong) UILabel *aLabel;

它使用 strong,相反,我在 RayWenderlich 網(wǎng)站上閱讀了一個(gè)教程,上面寫著:

<塊引用>

但是對(duì)于這兩個(gè)特定的屬性,我還有其他計(jì)劃.代替strong,我們將它們聲明為weak.

@property (nonatomic, weak) IBOutlet UITableView *tableView;@property (nonatomic, weak) IBOutlet UISearchBar *searchBar;

<塊引用>

Weak 是所有 outlet 屬性的推薦關(guān)系.這些視圖對(duì)象已經(jīng)是視圖控制器視圖的一部分層次結(jié)構(gòu),不需要在其他地方保留.最大的優(yōu)勢(shì)聲明你的 outlet weak 的好處是它可以節(jié)省你編寫viewDidUnload 方法.

目前我們的 viewDidUnload 看起來(lái)像這樣:

- (void)viewDidUnload{[超級(jí) viewDidUnload];self.tableView = nil;self.searchBar = nil;音效 = 無(wú);}

<塊引用>

您現(xiàn)在可以將其簡(jiǎn)化為以下內(nèi)容:

- (void)viewDidUnload{[超級(jí) viewDidUnload];音效 = 無(wú);}

所以使用weak,而不是strong,并去掉videDidUnload中的設(shè)置為nil,代替Xcode使用strong,并在 viewDidUnload 中使用 self... = nil.

我的問(wèn)題是:我什么時(shí)候必須使用strong,什么時(shí)候使用weak?我也想用于部署目標(biāo) iOS 4,那么我什么時(shí)候必須使用 unsafe_unretain?當(dāng)使用 strongweakunsafe_unretain 與 ARC 時(shí),任何人都可以通過(guò)一個(gè)小教程幫助我很好地解釋我?

解決方案

經(jīng)驗(yàn)法則

當(dāng)父對(duì)象引用子對(duì)象時(shí),您應(yīng)該使用 strong 引用.當(dāng)子對(duì)象具有對(duì)其父對(duì)象的引用時(shí),您應(yīng)該使用 weak 引用或 unsafe_unretained 引用(如果前者不可用).一個(gè)典型的場(chǎng)景是當(dāng)您與代表打交道時(shí).例如,UITableViewDelegate 不保留包含表格視圖的控制器類.

這里有一個(gè)簡(jiǎn)單的模式來(lái)展示主要概念.

假設(shè)第一個(gè) A、B 和 C 是 strong 引用.特別是,C 對(duì)其父級(jí)有一個(gè) strong 引用.當(dāng) obj1 被釋放(某處)時(shí),A 引用不再存在,但您有泄漏,因?yàn)?obj1 和 obj2 之間存在循環(huán).就保留計(jì)數(shù)而言(僅用于解釋目的),obj1 的保留計(jì)數(shù)為 2(obj2 有一個(gè) strong 引用),而 obj2 有一個(gè)保留計(jì)數(shù)of 1. 如果 obj1 被釋放,它的保留計(jì)數(shù)現(xiàn)在是 1,并且它的 dealloc 方法沒有被調(diào)用.obj1 和 obj2 仍然保留在內(nèi)存中,但沒有人引用它們:Leak.

反過(guò)來(lái)說(shuō),如果只有 A 和 B 是 strong refs 并且 C 被限定為 weak 一切都可以.你沒有泄漏.事實(shí)上,當(dāng) obj1 被釋放時(shí),它也釋放了 obj2.就保留計(jì)數(shù)而言,obj1 的保留計(jì)數(shù)為 1,obj2 的保留計(jì)數(shù)為 1.如果 obj1 被釋放,它的保留計(jì)數(shù)現(xiàn)在為 0,并調(diào)用其 dealloc 方法.obj1 和 obj2 從內(nèi)存中刪除.

一個(gè)簡(jiǎn)單的建議:在處理 ARC 時(shí)開始考慮對(duì)象圖.

關(guān)于您的第一個(gè)問(wèn)題,當(dāng)您處理 XIB 時(shí),兩種解決方案都有效.通常在處理內(nèi)存周期時(shí)會(huì)使用 weak 引用.關(guān)于 XIBs 文件,如果你使用 strong 你需要在 viewDidUnload 中設(shè)置 nil 因?yàn)槿绻悴贿@樣做,在內(nèi)存不足的情況下,您可能會(huì)導(dǎo)致意外泄漏.您不會(huì)在 dealloc 中釋放它們,因?yàn)?ARC 會(huì)為您完成.weak 不需要這種處理,因?yàn)楫?dāng)目標(biāo)對(duì)象被銷毀時(shí),這些值會(huì)自動(dòng)設(shè)置為 nil.不再有懸空指針.

如果你有興趣,我真的建議你閱讀friday-qa-2012-04-13-nib-memory-management by Mike Ash.

關(guān)于你的第二個(gè)問(wèn)題,如果你需要支持 iOS 4,你必須使用 unsafe_unretained 而不是 weak.

在 SO 中有很多問(wèn)題/答案.這里是主要的:

如何在使用 ARC 并針對(duì) iOS 4.0 時(shí),我應(yīng)該替換弱引用嗎?

什么樣的Objective-C 中的自動(dòng)引用計(jì)數(shù)不能防止或減少泄漏嗎?

使用 ARC、生命周期限定符 assign 和 unsafe_unretained

strong/weak/retain/unsafe_unretained/assign

希望對(duì)您有所幫助.

更新

根據(jù) shaunlim 的評(píng)論,從 iOS 6 開始,viewDidUnload 方法已被棄用.這里我真的建議看看 Rob 的回答:iOS 6 - viewDidUnload migrate to didReceiveMemoryWarning?.

I have switched my project to ARC, and I don't understand if I have to use strong or weak for IBOutlets. Xcode do this: in interface builder, if a create a UILabel for example and I connect it with assistant editor to my ViewController, it create this:

@property (nonatomic, strong) UILabel *aLabel;

It uses the strong, instead I read a tutorial on RayWenderlich website that say this:

But for these two particular properties I have other plans. Instead of strong, we will declare them as weak.

@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, weak) IBOutlet UISearchBar *searchBar;

Weak is the recommended relationship for all outlet properties. These view objects are already part of the view controller’s view hierarchy and don’t need to be retained elsewhere. The big advantage of declaring your outlets weak is that it saves you time writing the viewDidUnload method.

Currently our viewDidUnload looks like this:

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.tableView = nil;
    self.searchBar = nil;
    soundEffect = nil;
}

You can now simplify it to the following:

- (void)viewDidUnload
{
    [super viewDidUnload];
    soundEffect = nil;
}

So use weak, instead of the strong, and remove the set to nil in the videDidUnload, instead Xcode use the strong, and use the self... = nil in the viewDidUnload.

My question is: when do I have to use strong, and when weak? I want also use for deployment target iOS 4, so when do I have to use the unsafe_unretain? Anyone can help to explain me well with a small tutorial, when use strong, weak and unsafe_unretain with ARC?

解決方案

A rule of thumb

When a parent has a reference to a child object, you should use a strong reference. When a child has a reference to its parent object, you should use a weak reference or a unsafe_unretained one (if the former is not available). A typical scenario is when you deal with delegates. For example, a UITableViewDelegate doesn't retain a controller class that contains a table view.

Here a simple schema to present the main concepts.

Suppose first A,B and C are strong references. In particular, C has a strong ref to its parent. When obj1 is released (somewhere), the A reference doesn't exist anymore but you have a leak since there is a cycle between obj1 and obj2. Speaking in terms of retain counts (only for explain purposes), obj1 has a retain count of 2 (obj2 has a strong reference to it), while obj2 has a retain count of 1. If obj1 is released, its retain count is now 1 and its dealloc method is not called. obj1 and obj2 still remain in memory but no one has a reference to them: Leak.

On the contary, if only A and B are strong refs and C is qualified as weak all is ok. You have no leaks. In fact, when obj1 is released, it also releases obj2. Speaking in terms of retain counts, obj1 has a retain count of 1, obj2 has a retain count of 1. If obj1 is released, its retain count is now 0 and its dealloc method is called. obj1 and obj2 are removed from memory.

A simple suggestion: Start to think in terms of object graph when you deal with ARC.

About your first question, both solutions are valid when you deal with XIBs. In general weak references are used when you deal with memory cycles. Concerning XIBs files, if you use strong you need to set nil in viewDidUnload since if you don't do it, in memory low conditions, you could cause unexpected leaks. You don't release them in dealloc because ARC will do it for you. weak instead doesn't need that treatment since, when the target object is destroyed, those values are set as nil automatically. No dangling pointers anymore.

If you are interested in, I really suggest you to read friday-qa-2012-04-13-nib-memory-management by Mike Ash.

About your second question, if you need to support iOS 4, instead of weak you have to use unsafe_unretained.

Within SO there are a lot of questions/answers. Here the main ones:

How do I replace weak references when using ARC and targeting iOS 4.0?

What kind of leaks does automatic reference counting in Objective-C not prevent or minimize?

using ARC, lifetime qualifier assign and unsafe_unretained

strong / weak / retain / unsafe_unretained / assign

Hope that helps.

Update

As per shaunlim's comment, starting from iOS 6 viewDidUnload method is deprecated. Here I really suggest to see Rob's answer: iOS 6 - viewDidUnload migrate to didReceiveMemoryWarning?.

這篇關(guān)于IBOutlet 和其他的弱或強(qiáng)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Using Instruments to test an iOS app without having source code to the application(在沒有應(yīng)用程序源代碼的情況下使用 Instruments 測(cè)試 iOS 應(yīng)用程序)
KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自動(dòng)運(yùn)行/壓力測(cè)試 iOS 應(yīng)用程序以找出罕見 UI 錯(cuò)誤的原因?)
Can#39;t change target membership visibility in Xcode 4.5(無(wú)法更改 Xcode 4.5 中的目標(biāo)成員身份可見性)
UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合單元格表視圖靜態(tài)和動(dòng)態(tài)單元格中處理單元格選擇)
How to remove Address Bar in Safari in iOS?(如何在 iOS 中刪除 Safari 中的地址欄?)
iOS 5 SDK is gone after upgrade to Xcode 4.5(升級(jí)到 Xcode 4.5 后,iOS 5 SDK 消失了)
主站蜘蛛池模板: 国产一区91在线 | 国产在线精品一区二区三区 | 中文字幕不卡在线88 | 日韩精品成人免费观看视频 | 午夜三级视频 | 免费高清成人 | 动漫www.被爆羞羞av44 | 精品国产第一区二区三区 | 成人三级av | 在线一区视频 | 91一区二区三区 | 欧美一二三四成人免费视频 | 日韩免费一区二区 | 国产综合久久 | 污污免费网站 | k8久久久一区二区三区 | 隔壁老王国产在线精品 | 国产精品亚洲一区二区三区在线观看 | 免费在线视频a | 久久不卡 | h视频在线免费 | 热久色 | 亚洲精品视频一区二区三区 | 精品成人av | 日韩免费高清视频 | 久久久女| 黄免费观看视频 | 日韩有码一区二区三区 | 欧美精品片| 欧美一区二区三区在线 | 久久久精品网 | 日本成人三级电影 | 欧美一区二区三 | 欧美一区中文字幕 | 午夜欧美日韩 | 亚洲国产精品久久久 | 成人免费在线网 | 欧美日韩精品一区二区天天拍 | 午夜精品久久久久久久久久久久久 | 日韩高清av | 国产成人免费视频 |