久久久久久久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 消失了)
主站蜘蛛池模板: 四虎永久在线视频 | 在线一级片| 综合色婷婷 | 又黄又爽又色视频 | 欧美日韩精品久久 | 亚洲第二区 | 一级特黄妇女高潮 | 人人干人人澡 | 日韩国产在线 | 日本免费一级片 | 久久黄色一级片 | 国产精品久久久久久久久久久久久 | 久久99精品国产麻豆91樱花 | 日韩国产在线 | 怡红院久久 | 永久在线 | 看毛片网站 | www.伊人| www.三级| 国产一级在线 | 一区二区久久久 | 日韩视频免费大全中文字幕 | 中文字幕理论片 | 黄色一级在线观看 | av手机天堂 | 日本在线天堂 | 亚洲特级毛片 | 久久精品国产一区 | 日韩精品一区在线 | 91av在线看 | 欧美激情久久久 | 国产精品成人在线观看 | av在线免费观看网站 | 欧美在线小视频 | 欧美日韩在线看 | 国产成人免费在线视频 | aaaaaabbbbbb毛片| 超碰av在线 | 亚洲成人av在线播放 | 午夜精品视频 | 中国免费av |