ios - 键值编码说明

标签 ios objective-c key-value-coding

从 Apple 的文档中,我发现了以下内容:

Setting values using key paths is not the same as setting them using Objective-C properties. You cannot use property notation to set transform values. You must use the setValue:forKeyPath: method with the preceding key path strings.

根据我的理解,我们必须拥有我们的 ivar 的属性才能使用 KVC。

但是从上面这段话来看,好像不是这么说的:

使用键路径设置值不同于使用 Objective-C 属性。您不能使用属性符号来设置 转换值。

谁能告诉我为什么?也许我遗漏了什么,因为一直以来我的理解是我们必须有属性才能使用 KVC。

最佳答案

From my understanding, we must have properties of our ivars in order to use KVC.

这是不正确的。 KVC 将使用属性生成的访问器方法,或其他(适当命名的)访问器方法,如果它们存在,但它们不是必需的。如果它们不存在,KVC 将尝试直接访问实例变量。

检查 this Apple ref有关 KVC 如何搜索要更新的 key 的说明。

关于ios - 键值编码说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22738059/

相关文章:

objective-c - 函数返回 block 的内存管理

objective-c - 自定义类 NSObject 不符合键值编码

ios - 带字典的 JSON - 嵌套对象转换为字符串并显示

Cocoa 绑定(bind)到数组 Controller 中的特定项目

iphone - UIButton Transition - 跨屏幕运行

iOS 位置管理器 - isAuthorizedForPreciseLocation 似乎不存在

ios - 从 uicollectionciewcell 中的 uitextfield 更新和获取文本

iphone - 如何在objectiveC类中使用struct和union?

ios - 圆形 UIImageView

ios - Xcode 11.0 具有需要使用开发证书签名的权利。在 Signing & Capabilities 编辑器中启用开发签名