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

ARC、塊和保留周期

ARC, Blocks and Retain Cycles(ARC、塊和保留周期)
本文介紹了ARC、塊和保留周期的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

使用 ARC 處理面向 4.0 和 5.0 的 iOS 項目.

Working on an iOS project that targets 4.0 and 5.0, using ARC.

遇到與塊、ARC 和從塊外引用對象相關的問題.這是一些代碼:

Running into an issue related to blocks, ARC and referencing an object from outside the block. Here's some code:

 __block AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
   [operation setCompletionBlock:^ {
       if ([operation isCancelled]) {
           return;
       }

... do stuff ...

operation = nil;
}];

在這種情況下,編譯器會發出警告,在塊中使用操作"將導致保留循環.在 ARC 下,__block 現在保留了變量.

In this case, the compiler gives a warning that using 'operation' in the block is going to lead to a retain cycle. Under ARC, __block now retains the variable.

如果我添加 __unsafe_unretained,編譯器會立即釋放該對象,所以顯然這不起作用.

If I add __unsafe_unretained, the compiler releases the object immediately, so obviously that won't work.

我的目標是 4.0,所以我不能使用 __weak.

I'm targeting 4.0 so I can't use __weak.

我試著做這樣的事情:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
__block __unsafe_unretained AFHTTPRequestOperation *weakOperation = operation;

但是雖然 weakOperation 不是 nil,但它的任何屬性都不會在塊內填充.

but while weakOperation isn't nil, none of it's properties are populated when inside the block.

鑒于上面列出的項目限制,處理這種情況的最佳方法是什么?

What's the best way to handle this situation given the project constraints listed above?

推薦答案

假設進度保證,保留周期可能正是您想要的.您在塊結束時顯式中斷保留循環,因此它不是永久保留循環:當調用塊時,循環被中斷.

Assuming progress guarantees, a retain cycle might be exactly what you want. You explicitly break the retain cycle at the end of the block, so it's not a permanent retain cycle: when the block is called, the cycle is broken.

但是,如果您有其他東西保持操作,您可以將引用存儲到 __weak__unsafe_unretained 變量中,然后在您的塊中使用它.除非您出于某種原因需要在塊期間更改變量的綁定,否則無需對變量進行 __block 限定;由于您不再需要中斷循環,因此您不需要為弱變量分配任何內容.

If you have something else keeping the operation around, though, you can store a reference into either a __weak or __unsafe_unretained variable and then use that from within your block. There's no need to __block-qualify the variable unless you for some reason need to change the variable's binding during the block; since you don't have a retain cycle to break any more, you shouldn't need to assign anything to the weak variable.

這篇關于ARC、塊和保留周期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Using Instruments to test an iOS app without having source code to the application(在沒有應用程序源代碼的情況下使用 Instruments 測試 iOS 應用程序)
KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自動運行/壓力測試 iOS 應用程序以找出罕見 UI 錯誤的原因?)
Can#39;t change target membership visibility in Xcode 4.5(無法更改 Xcode 4.5 中的目標成員身份可見性)
UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合單元格表視圖靜態和動態單元格中處理單元格選擇)
How to remove Address Bar in Safari in iOS?(如何在 iOS 中刪除 Safari 中的地址欄?)
iOS 5 SDK is gone after upgrade to Xcode 4.5(升級到 Xcode 4.5 后,iOS 5 SDK 消失了)
主站蜘蛛池模板: 国产午夜精品一区二区 | 精品入口麻豆88视频 | 亚洲综合一区二区三区 | 99精品久久久国产一区二区三 | 国产激情在线 | 亚洲男人天堂av | 亚洲免费在线播放 | 一级一级一级毛片 | 欧美一级免费观看 | 极品销魂美女一区二区 | 亚洲毛片在线 | 成人午夜免费福利视频 | 亚洲成人网在线播放 | 毛片久久久| 欧美福利| 国产精品亚洲一区 | 91精品国产综合久久香蕉922 | 精品国产免费人成在线观看 | 中文字幕视频在线观看 | 黄色在线免费观看视频网站 | 在线播放亚洲 | 色偷偷噜噜噜亚洲男人 | 日韩中文一区二区三区 | 午夜免费网站 | 国产天堂 | 久久久久国产 | 毛片a级毛片免费播放100 | 久草新在线| 亚洲永久精品国产 | 国产成人精品在线 | 中文字幕日韩av | 一级黄色日本片 | 日本午夜视频 | 美女国产| 狠狠躁夜夜躁人人爽天天高潮 | 成人国产精品久久久 | 91视频88av| 国产成人福利视频在线观看 | 九九导航 | 伊人最新网址 | 成年人的视频免费观看 |