使用 UIKit 的 KVO 有多可靠 [英] How reliable is KVO with UIKit

查看:23
本文介绍了使用 UIKit 的 KVO 有多可靠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重要提示:并非所有课程都是所有属性都符合 KVO.你可以确保您自己的课程是按照以下步骤符合 KVOKVO 合规性"中描述.Apple 提供的典型属性框架仅在以下情况下才符合 KVO它们被记录在案.

Important: Not all classes are KVO-compliant for all properties. You can ensure your own classes are KVO-compliant by following the steps described in "KVO Compliance." Typically properties in Apple-supplied frameworks are only KVO-compliant if they are documented as such.

这句话让我很困惑.我们不能对 UIKit 对象使用 KVO 吗?我不记得看到任何属性被记录为符合 KVO 标准.尽管另有说明,但我可以使用具有许多属性的 KVO.这是否意味着我不能依赖它?

This statement leaves me confused. Can't we use KVO for UIKit objects at all? I don't remember seeing any property being documented as KVO compliant. Despite saying otherwise, I am able to use KVO with many properties. Does this mean that I can't rely on it?

任何对此的见解将不胜感激.

Any insight into this would be appreciated.

推荐答案

UIKit 大多不兼容 KVO.这主要是因为 UIView 充当 CALayer 的高级包装器,所以当你例如.改变 UIViewframe 属性,它会改变层 frame 但留下例如.UIViewbounds 属性保持不变,因此不会为 view.bounds 路径触发观察者,因为它永远不会真正改变.这会导致不符合 KVO.

UIKit is mostly NOT KVO compliant. This is mostly because UIView acts as high-level wrapper for CALayer, so when you eg. change the frame property of an UIView, it will change the layers frame but leave eg. the bounds property of the UIView untouched, so no observer will be triggered for the view.bounds path, because it never really changes. And this leads to non KVO compliance.

只有当属性被标记为 KVO 兼容时,你才能依赖它,否则它在一些奇怪的情况下将无法工作或中断.

Only if the property is marked as KVO compliant can you rely on this, otherwise it will not work or break in some weird cases.

这篇关于使用 UIKit 的 KVO 有多可靠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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