关于“确保 KVO 合规性”,
有一些官方定义似乎很难理解
为了被考虑
特定属性符合 KVO 标准,
班级必须确保以下内容;
类必须是键值编码
符合该财产的要求
确保 KVC 合规性中指定。
该类必须允许自动
观察员通知
财产,或实施手册
属性的键值观察。
谁能举出更具体的例子来更清楚地说明这一点?谢谢
Regarding "Ensuring KVO Compliance",
there are some official definition which seem like hard to understand
In order to be considered
KVO-compliant for a specific property,
a class must ensure the following;
-
The class must be key-value coding
compliant for the property as
specified in Ensuring KVC Compliance.
-
The class must allow automatic
observer notifications for the
property, or implement manual
key-value observing for the property.
Who can give more specific examples to make this more clear ? Thanks
发布评论
评论(2)
看看 确保 KVO 合规性键值观察编程指南的自动与手动支持部分。合规性本质上是通过遵循方法和/或变量的命名约定来实现的。
根据我的经验,KVO 倾向于“正常工作”,这很好。
Take a look at Ensuring KVO Compliance the Automatic Versus Manual Support section of the Key-Value Observing Programming Guide. Compliance is essentially achieved by following naming conventions for methods and/or ivars.
In my experience KVO tends to 'just work', which is nice.
当您使用 @property 标签 创建 getter/setter 对于一对一关系,魔法是自动连接的,您只需要 addObserver... 并实现observe... 方法来捕获更新。
真正的挑战是了解如何实现对多键值合规性以使可变集或数组正常工作。 此处的文档是关键,并且要理解有两个部分......索引集合和无序集合。它确实帮助我理解,如果我的@property是一个NSMutableArray,你需要查看索引区域中的方法,如果你的@property是一个NSMutableSet,你需要查看Unordered文档。
即使您不打算使用某些必需方法,您也需要实现它们。
When you use @property tags to create getters/ setters the magic is auto wired for To-One relationships and you only need to addObserver... and implement the observe... method to catch the updates.
The real challenge is understanding how you implement To-Many Key Value Compliance to make a mutable set or array work. The documentation here is key and the understanding that there are two sections... Indexed collections and Unordered collections. It really helped me to understand that if my @property is an NSMutableArray you need to look at the methods in the Indexed area and if your @property is a NSMutableSet you need to look at the Unordered documentation.
There are Required methods that you need to implement even if you don't plan to use them.