KVO 与 UIKit 的可靠性如何
重要提示:并非所有课程都是 所有属性均符合 KVO 标准。你 可以确保您自己的课程 按照以下步骤符合 KVO 标准 “KVO 合规性”中描述。 通常是 Apple 提供的属性 框架仅在以下情况下才符合 KVO: 它们被记录下来。
这句话让我很困惑。我们不能对 UIKit 对象使用 KVO 吗?我不记得有任何财产被记录为符合 KVO 标准。尽管另有说法,我可以使用具有许多属性的 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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UIKit 大部分不兼容 KVO。这主要是因为
UIView
充当CALayer
的高级包装器,因此当您例如。更改UIView
的frame
属性,它将更改层frame
但保留例如。UIView
的bounds
属性保持不变,因此不会触发view.bounds
路径的观察者,因为它永远不会真正改变。这会导致不符合 KVO 要求。只有当该属性被标记为符合 KVO 规范时,您才能依赖它,否则它将无法工作或在某些奇怪的情况下崩溃。
UIKit is mostly NOT KVO compliant. This is mostly because
UIView
acts as high-level wrapper forCALayer
, so when you eg. change theframe
property of anUIView
, it will change the layersframe
but leave eg. thebounds
property of theUIView
untouched, so no observer will be triggered for theview.bounds
path, because it never really changes. And this leads to non KVO compliance.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.