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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With