问题描述
如果是这样,那么在Objective-C中使用键值观察时是否没有其他键差异?
If so, are there any key differences that weren't otherwise present when using key-value observation in Objective-C?
推荐答案
(编辑以添加新信息):考虑使用Combine框架是否可以帮助您完成所需的工作,而不是使用KVO
(Edited to add new info): consider whether using the Combine framework can help you accomplish what you wanted, rather than using KVO
是,不是. KVO一直以来都在处理NSObject子类.它不适用于不继承NSObject的类. Swift(目前至少)没有自己的本地观察系统.
Yes and no. KVO works on NSObject subclasses much as it always has. It does not work for classes that don't subclass NSObject. Swift does not (currently at least) have its own native observation system.
(请参阅注释,了解如何将其他属性公开为ObjC,以便KVO在它们上起作用)
(See comments for how to expose other properties as ObjC so KVO works on them)
请参见 Apple文档以获取完整示例.
See the Apple Documentation for a full example.
这篇关于Swift中是否可以使用键值观察(KVO)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!