找不到“NSKeyValueObserving”的协议声明。但为什么?

发布于 2024-11-29 04:17:24 字数 184 浏览 4 评论 0原文

@interface ThreadsViewController : UIViewController <NSKeyValueObserving>
{
}   

错误:找不到“NSKeyValueObserving”的协议声明。

我正在使用基础框架。为什么我会出现这个错误?

@interface ThreadsViewController : UIViewController <NSKeyValueObserving>
{
}   

Error:Cannot find protocol declaration for 'NSKeyValueObserving'.

I'm using Foundation framework. Why do I have this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等风来 2024-12-06 04:17:24

NSKeyValueObserving 是一个 非正式协议

非正式协议是 NSObject 上的一个类别,它隐式地使几乎所有对象都采用该协议。 (...) 非正式协议中方法的实施是可选的。在调用方法之前,调用对象会检查目标对象是否实现了该方法。

您不能采用使用尖括号语法的非正式协议,即正式协议。使用非正式协议,您只需实现协议方法即可。您尝试在 @interface 块中遵守 NSKeyValueObserving 协议:

@interface ThreadsViewController : UIViewController <NSKeyValueObserving>

是编译器抱怨的原因。

来自 协议

由于是非正式的,在类别中声明的协议没有得到太多的语言支持。编译时没有类型检查,运行时也没有检查对象是否符合协议。

NSKeyValueObserving is an informal protocol:

An informal protocol is a category on NSObject, which implicitly makes almost all objects adopters of the protocol. (...) Implementation of the methods in an informal protocol is optional. Before invoking a method, the calling object checks to see whether the target object implements it.

You can not adopt an informal protocol using the angle brackets syntax, that is for formal protocols. With informal protocols, you simply implement the protocol methods. Your attempt to conform to the protocol NSKeyValueObserving in the @interface block:

@interface ThreadsViewController : UIViewController <NSKeyValueObserving>

is the reason why the compiler is complaining.

From Protocols:

Being informal, protocols declared in categories don’t receive much language support. There’s no type checking at compile time nor a check at runtime to see whether an object conforms to the protocol.

暗地喜欢 2024-12-06 04:17:24

您应该检查 NSKeyValueObserving 的文档。您可以在顶部找到框架所在的位置。只需确认您将该框架包含在您的项目中即可。

You should check the documentation for NSKeyValueObserving. In that you can find at top that in which framework resides. Just confirm that you are including that framework in your project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文