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

對 ARC 下對象的弱引用 (__unsafe_unretained) 的 NSArr

NSArray of weak references (__unsafe_unretained) to objects under ARC(對 ARC 下對象的弱引用 (__unsafe_unretained) 的 NSArray)
本文介紹了對 ARC 下對象的弱引用 (__unsafe_unretained) 的 NSArray的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我需要在 NSArray 中存儲對對象的弱引用,以防止保留循環.我不確定要使用的正確語法.這是正確的方法嗎?

I need to store weak references to objects in an NSArray, in order to prevent retain cycles. I'm not sure of the proper syntax to use. Is this the correct way?

Foo* foo1 = [[Foo alloc] init];
Foo* foo2 = [[Foo alloc] init];

__unsafe_unretained Foo* weakFoo1 = foo1;
__unsafe_unretained Foo* weakFoo2 = foo2;

NSArray* someArray = [NSArray arrayWithObjects:weakFoo1, weakFoo2, nil];

請注意,我需要支持 iOS 4.x,因此使用 __unsafe_unretained 而不是 __weak.

Note that I need to support iOS 4.x, thus the __unsafe_unretained instead of __weak.

編輯(2015-02-18):

EDIT (2015-02-18):

對于那些想要使用真正的 __weak 指針(而不是 __unsafe_unretained)的人,請查看以下問題:ARC下歸零弱引用的集合

For those wanting to use true __weak pointers (not __unsafe_unretained), please check out this question instead: Collections of zeroing weak references under ARC

推薦答案

正如 Jason 所說,你不能讓 NSArray 存儲弱引用.實現 Emile 的建議的最簡單的方法是:將一個對象包裝在另一個存儲弱引用的對象中:

As Jason said, you can't make NSArray store weak references. The easiest way to implement Emile's suggestion of wrapping an object inside another object that stores a weak reference to it is the following:

NSValue *value = [NSValue valueWithNonretainedObject:myObj];
[array addObject:value];

另一個選項:一個 類別,它使 NSMutableArray 可選擇地存儲弱引用.

Another option: a category that makes NSMutableArray optionally store weak references.

請注意,這些是不安全的未保留"引用,而不是自歸零弱引用.如果在對象被釋放后數組仍然存在,你就會有一堆垃圾指針.

Note that these are "unsafe unretained" references, not self-zeroing weak references. If the array is still around after the objects are deallocated, you'll have a bunch of junk pointers.

這篇關于對 ARC 下對象的弱引用 (__unsafe_unretained) 的 NSArray的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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 消失了)
Having trouble creating UIImage from CIImage in iOS5(在 iOS5 中從 CIImage 創建 UIImage 時遇到問題)
主站蜘蛛池模板: 一级免费在线视频 | 最新av在线播放 | 日韩一区二区三区在线视频 | 国产精品久久久久久久久久久久久久 | 激情av在线 | 国产精品久久久久久婷婷天堂 | 日韩视频a | 久久人人爽人人爽人人片av免费 | 亚洲 欧美 日韩在线 | 91免费在线看 | www.国产精 | 日韩精品在线网站 | 91视频精选 | 操人视频在线观看 | 国产色| 男女羞羞在线观看 | 亚洲精品久久久久久久久久久久久 | 精品欧美一区二区三区 | 一级视频黄色 | 精品视频一区二区三区在线观看 | 国产精品美女一区二区 | 久久精品欧美一区二区三区麻豆 | 亚洲自拍偷拍视频 | 久久久久久国产精品免费免费男同 | 男女爱爱网站 | 99爱在线 | 久久精品亚洲精品国产欧美 | 日韩精品一区二区三区在线观看 | 国产激情| 久久日韩精品一区二区三区 | 网色| 免费精品 | 成人在线观看免费 | 中国一级大毛片 | 亚洲精品成人 | 国产精品99久久久久 | 视频精品一区二区三区 | 91免费在线视频 | 久久99精品久久久久久秒播九色 | 伊人精品一区二区三区 | a级毛片基地 |