如何在设计数据模型时使用NSKeyValueCoding协议?

时间:2021-09-30 22:11:51

I have been trying to create a data model class while following the NSKeyValueCoding protocol. I keep getting the following error: "Cannot find protocol declaration" And when I try to #import "NSKeyValueCoding.h" I get the file not found error.

我一直在尝试按照NSKeyValueCoding协议创建数据模型类。我不断收到以下错误:“找不到协议声明”当我尝试#import“NSKeyValueCoding.h”时,我收到文件未找到错误。

Where can I find this protocol OR how else can it be implemented?

我在哪里可以找到这个协议或者如何实现它?

1 个解决方案

#1


6  

NSKeyValueCoding is part of Foundation. So if you #import <Foundation/Foundation.h> you already included the header.

NSKeyValueCoding是Foundation的一部分。因此,如果您#import ,您已经包含了标题。

NSKeyValueCoding is an informal protocol. That means that classes do not explicitly conform to it. On the contrary: The protocol claims that the class it is declared upon, NSObject in this case, does understand all it's methods.

NSKeyValueCoding是一种非正式协议。这意味着类没有明确地符合它。相反:协议声称它声明的类,在这种情况下是NSObject,它确实理解它的所有方法。

So: You cannot write a (NSObject derived) class in Cocoa that does not conform to KVC.

所以:你不能在Cocoa中编写一个不符合KVC的(NSObject派生)类。

But: You can add non KVC-compliant properties to this class, for example by using funny names for accessors. It might make sense for an implementation to follow the rules for being KVC compliant or even KVO compliant.

但是:您可以向此类添加非KVC兼容属性,例如通过为访问者使用有趣的名称。对于实现遵循符合KVC或甚至符合KVO的规则可能是有意义的。

#1


6  

NSKeyValueCoding is part of Foundation. So if you #import <Foundation/Foundation.h> you already included the header.

NSKeyValueCoding是Foundation的一部分。因此,如果您#import ,您已经包含了标题。

NSKeyValueCoding is an informal protocol. That means that classes do not explicitly conform to it. On the contrary: The protocol claims that the class it is declared upon, NSObject in this case, does understand all it's methods.

NSKeyValueCoding是一种非正式协议。这意味着类没有明确地符合它。相反:协议声称它声明的类,在这种情况下是NSObject,它确实理解它的所有方法。

So: You cannot write a (NSObject derived) class in Cocoa that does not conform to KVC.

所以:你不能在Cocoa中编写一个不符合KVC的(NSObject派生)类。

But: You can add non KVC-compliant properties to this class, for example by using funny names for accessors. It might make sense for an implementation to follow the rules for being KVC compliant or even KVO compliant.

但是:您可以向此类添加非KVC兼容属性,例如通过为访问者使用有趣的名称。对于实现遵循符合KVC或甚至符合KVO的规则可能是有意义的。