iPhone how to check the type of an Object?

后端 未结 3 1444
长情又很酷
长情又很酷 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:08

    There are some methods on NSObject that allow you to check classes.

    First there's -class which will return the Class of your object. This will return either AViewController or BViewController.

    Then there's two methods, -isKindofClass: and isMemberOfClass:.

    -isKindOfClass: will compare the receiver with the class passed in as the argument and return true or false based on whether or not the class is the same type or a subclass of the given class.

    -isMemberOfClass: will compare the receiver with the class passed in as the argument and return true or false based on whether or not the class is strictly the same class as the given class.

提交回复
热议问题