iPhone how to check the type of an Object?

后端 未结 3 1449
长情又很酷
长情又很酷 2020-12-24 00:40

I want to check the type of an Object. How can I do that?

The scenario is I\'m getting an object. If that object is of type A then do some operations. If it is of ty

3条回答
  •  难免孤独
    2020-12-24 01:06

    A more common pattern in Objective-C is to check if the object responds to the methods you are interested in. Example:

    if ([object respondsToSelector:@selector(length)]) {
        // Do something
    }
    
    if ([object conformsToProtocol:@protocol(NSObject)]) {
        // Do something
    }
    

提交回复
热议问题