久久久久久久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手机在线观看 | 久久99国产精一区二区三区 | 欧美a在线看 | 中文在线一区二区 | 国产精品美女一区二区三区 | 国产高清视频一区二区 | 欧美成人一区二区 | 亚洲一一在线 | 日韩区| 国产精品视频一区二区三区四区国 | 99免费精品视频 | 精品国产精品三级精品av网址 | 亚洲精品一区二三区不卡 | 91电影在线播放 | 天天操操操操操 | 日韩亚洲一区二区 | 韩国毛片视频 | 中文字幕精品一区二区三区精品 | 亚洲午夜视频 | 成人在线精品 | 激情91| 久久久久成人精品 | 2021狠狠干 | 国产一区二区三区在线看 | 91在线免费视频 | 91精品国产一区二区 | 大象视频一区二区 | 欧美在线观看网站 | 久久久久久精 | 婷婷亚洲综合 | 国产精品永久久久久久久www | 国产精品久久久久久久久污网站 | 亚洲综合在 | 毛片大全 | 伊伊综合网 | 国产激情视频在线 | 亚洲一区二区不卡在线观看 | 偷派自拍 |