本文介绍了id< protocol>之间的区别和NSObject<协议>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中, id< protocol>或NSObject< protocol>

In Objective-C, id<protocol> or NSObject<protocol> are frequently used for delegate declaration.

id和NSObject之间的主要区别是什么?
你什么时候想使用一个对方?

What are the main differences between id and NSObject?When do you want to use one versus the other?

推荐答案

id< protocol> obj 是符合指定协议的任何对象的声明。
您可以将给定协议的任何消息发送到对象(或从< protocol> 继承的协议
)。

id<protocol> obj is the declaration for any object that conforms to the specified protocol.You can send any message from the given protocol to the object (or from the protocolsthat <protocol> inherits from).

NSObject< protocol> * obj 是任何对象的声明,


  • 符合给定的协议,

  • 派生自 NSObject

  • conforms to the given protocol, and
  • is derived from NSObject.

这意味着在第二种情况下,您可以将 NSObject class
中的任何方法发送到对象,例如

That means that in the second case, you can send any methods from the NSObject classto the object, for example

id y = [obj copy];

这将在第一种情况下给出编译器错误。

which would give a compiler error in the first case.

第二个声明还意味着 obj 符合 NSObject 协议
但是如果< protocol> 是从NSObject协议派生的,那么这不会有什么区别:

The second declaration also implies that obj conforms to the NSObject protocol.But this makes no difference if <protocol> is derived from the NSObject protocol:

@protocol protocol <NSObject>

这篇关于id&lt; protocol&gt;之间的区别和NSObject&lt;协议&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 23:28