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

后端 未结 5 1590
太阳男子
太阳男子 2021-01-03 22:48

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

id tracker = [[GAI sharedInstance] defaultTrack         


        
相关标签:
5条回答
  • 2021-01-03 23:17

    1.The id type is designed to be a generic type which can hold any object type (in other words, id does not work with primitive types, such as ints and BOOLs).

    2.Imagine that you had a class that processed some external data. You don’t know or don’t care where the data comes from, but you should be prepared to handle many different types. Your data might come from a text file, where the contents might be read and passed into your method as an NSString. You might have to process the data in your own program somewhere else, and the data then would be as an NSArray or NSSet. Alternatively, the data could be coming from the internet as a JSON response, which must be parsed into an NSDictionary (don’t worry if you don’t know what JSON is…there’ll be something about this later down the road).

    - (void)processData:(id)someData {
    
        if ([someData isKindOfClass:[NSString class]])
            NSLog(@"input data is %@", someData);
    
        else if ([someData isKindOfClass:[NSArray class]]) {
            // Cast someData into an NSArray
            NSArray *dataArray = (NSArray *)someData;
            NSLog(@"First object in dataArray is %@", [dataArray objectAtIndex:0]);
        }
    
        else if ([someData isKindOfClass:[NSDictionary class]]) {
            // Cast someData into an NSDictionary
            NSDictionary *dataDict = (NSDictionary *)someData;
            NSLog(@"Keys in dataDict are %@", [dataDict allKeys]);
        }
    
        else if ([someData isKindOfClass:[NSData class]])
            NSLog(@"someData is a bag of bits.");
    
        else
            NSLog(@"someData is an unsupported type:\n%@", someData);
    }
    

    You can get more details on this link http://www.binpress.com/tutorial/learn-objectivec-objects-part-8-dynamic-typing/68

    0 讨论(0)
  • 2021-01-03 23:19

    When you use "id" it's like void *, you know it's a pointer but you don't know the type. When you use you mean the object does implement MyProtocol. You don't know the type of the object, it can NSObject, UIViewController, you don't care. You just want the object to implement a set of protocol. Finally, GAITracker means the type of the object is GAITracker.

    0 讨论(0)
  • 2021-01-03 23:24

    id

    id can hold any type of object.

    id<GAITracker> tracker

    This means the tracker is an id type object, which can hold objects those confirms to GAITracker protocol

    0 讨论(0)
  • 2021-01-03 23:40

    Here id is a pointer that hold an object.

    and tracker is an instance that conforms to GAITracker protocol.

    Protocols make it possible for two classes distantly related by inheritance to communicate with each other to accomplish a certain goal. They thus offer an alternative to subclassing.

    0 讨论(0)
  • 2021-01-03 23:42

    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.

    0 讨论(0)
提交回复
热议问题