Swift 中是否提供键值观察 (KVO)? [英] Is key-value observation (KVO) available in Swift?

查看:34
本文介绍了Swift 中是否提供键值观察 (KVO)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果是这样,在 Objective-C 中使用键值观察时是否存在任何不存在的关键差异?

If so, are there any key differences that weren't otherwise present when using key-value observation in Objective-C?

推荐答案

(Edited to add new info): 考虑使用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)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆