今天讲unsafe_unretained以及__unsafe_unretained的关键词使用方法。
Use Unsafe Unretained References for Some Classes
There are a few classes in Cocoa and Cocoa Touch that don’t yet support weak references, which means you can’t declare a weak property or weak local variable to keep track of them. These classes include NSTextView, NSFont and NSColorSpace; for the full list, see Transitioning to ARC Release Notes.
If you need to use a weak reference to one of these classes, you must use an unsafe reference. For a property, this means using the unsafe_unretained attribute:
@property (unsafe_unretained) NSObject *unsafeProperty;
For variables, you need to use __unsafe_unretained:
NSObject * __unsafe_unretained unsafeReference;
An unsafe reference is similar to a weak reference in that it doesn’t keep its related object alive, but it won’t be set to nil if the destination object is deallocated. This means that you’ll be left with a dangling pointer to the memory originally occupied by the now deallocated object, hence the term “unsafe.” Sending a message to a dangling pointer will result in a crash.
本文介绍了在ARC环境下,对于不支持弱引用的类如NSTextView等,如何使用unsafe_unretained关键字来声明属性和变量。

被折叠的 条评论
为什么被折叠?



