ios - 我怎么知道一个对象是否可以被弱引用?

标签 ios swift nsnotificationcenter

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/

相关文章:

nsnotificationcenter - Mountain Lion 上的同步 NSUserNotification 警报

ios - 目标之间的 Storyboard 中有不同的内容

ios - 如何以编程方式显示图像水平 UIscrollView

ios - 在非 View Controller 类中添加事件指示器

ios - Swift/iOS - 在较小的屏幕上触发 ScrollView

ios - 无法检测到键盘消失?

ios - 解析 XLSX 期间找不到 Xcode 文件错误

swift - RxSwift : What is the usage difference between BehaviorSubject and BehaviorRelay?

ios - 运行 iOS 模拟器时,信息和调试统一日志记录消息未出现在控制台中

ios - TabBarController 的 NSNotification