Foundation Release Notes for OS X v10.11 and iOS 9: NSNotificationCenter说:“如果对象可以被弱引用,则在释放期间将不再向观察者发送通知。”如何判断“一个对象是否可以被弱引用”?
最佳答案
在 Clang documentation 中有一些关于此的信息:
It is explicitly permitted for Objective-C classes to not support __weak references. It is undefined behavior to perform an operation with weak assignment semantics with a pointer to an Objective-C object whose class does not support __weak references.
Rationale
Historically, it has been possible for a class to provide its own reference-count implementation by overriding retain, release, etc. However, weak references to an object require coordination with its class’s reference-count implementation because, among other things, weak loads and stores must be atomic with respect to the final release. Therefore, existing custom reference-count implementations will generally not support weak references without additional effort. This is unavoidable without breaking binary compatibility.
A class may indicate that it does not support weak references by providing the objc_arc_weak_reference_unavailable attribute on the class’s interface declaration. A retainable object pointer type is weak-unavailable if is a pointer to an (optionally protocol-qualified) Objective-C class T where T or one of its superclasses has the objc_arc_weak_reference_unavailable attribute. A program is ill-formed if it applies the __weak ownership qualifier to a weak-unavailable type or if the value operand of a weak assignment operation has a weak-unavailable type.
此外,假设它仍然是最新的,Transitioning to ARC说:
Which classes don’t support weak references?
You cannot currently create weak references to instances of the following classes:
NSATSTypesetter, NSColorSpace, NSFont, NSMenuView, NSParagraphStyle, NSSimpleHorizontalTypesetter, and NSTextView.
关于ios - 我怎么知道一个对象是否可以被弱引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41354853/