Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does id<...> mean in Objective-C?

Tags:

I'm trying to use Google Analytics in an iOS application, and I saw this portion of code :

id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker]; 

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 for me?

like image 957
Mohamed Amine Avatar asked Nov 06 '13 12:11

Mohamed Amine


People also ask

What is id in Obj C?

id is a data type of object identifiers in Objective-C, which can be use for an object of any type no matter what class does it have. id is the final super type of all objects.

What is the primary use of the id type in Objective-C?

Dynamic typing allows us to declare a variable that is capable of storing any type of object, regardless of its class origins. This is achieved using the Objective-C id type. The idtype is a special, general purpose data type that can be assigned an object of any type.

What is id in Swift?

In Swift 3, the id type in Objective-C now maps to the Any type in Swift, which describes a value of any type, whether a class, enum, struct, or any other Swift type.

What is a protocol in Objective-C?

Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. This chapter describes the syntax to define a formal protocol, and explains how to mark a class interface as conforming to a protocol, which means that the class must implement the required methods.


1 Answers

The <> means that the object conforms to the protocol (pr protocols) inside the square brackets.

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.

like image 69
Fogmeister Avatar answered Sep 20 '22 20:09

Fogmeister