id< ...>是什么在Objective-C中意味着什么?
我正在尝试在iOS应用中使用Google Analytics,我看到了这部分代码:
I'm trying to use Google Analytics in an iOS application, and I saw this portion of code :
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
作为iOS开发的初学者,我不知道 ID< GAITracker>跟踪器和 GAITracker *跟踪器
。我在谷歌搜索过但没有找到解释。有人能澄清一下吗?
As a beginner in iOS development, I don't know what is the difference between id<GAITracker> tracker
and GAITracker *tracker
. I searched for it in Google but didn't find the explanation. Can someone clarify it to me ?
谢谢。
<>表示该对象符合方括号内的协议(pr协议)。
The <> means that the object conforms to the protocol (pr protocols) inside the square brackets.
在您的示例中,对象 tracker
可以是任何类型的对象,但它符合 GAITracker
协议。
On your example, the object tracker
can be any type of object but it conforms to the GAITracker
protocol.
因此,尽管如此它没有特定的类,您仍然可以调用该协议声明的方法和属性。
Therefore, although it doesn't have a specific class you can still call methods and properties on it that are declared by that protocol.