最終更新:2013-01-09 (水) 09:08:29 (4124d)  

unsafe_unretained
Top / unsafe_unretained

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html

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.