Objective-c:isKindOfClass 与 isMemberOfClass

前言

一些iOS初学者常常困惑于 isKindOfClass 与 isMemberOfClass 的用法,它们之间到底有什么区别呢?如下我将进行介绍。

定义

官方解释

  • isKindOfClass:Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.

  • isMemberOfClass:Returns a Boolean value that indicates whether the receiver is an instance of a given class.

作用

  • isKindOfClass:能够判断出对象是否为某类或其派生类的实例。

  • isMemberOfClass:能够判断出对象是否为某个特定类的实例。

使用

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

[dict isMemberOfClass:[NSDictionary class]];        // NO
[dict isMemberOfClass:[NSMutableDictionary class]]; // YES

[dict isKindOfClass:[NSDictionary class]]; // YES
[dict isKindOfClass:[NSArray class]];      // NO

你可能感兴趣的:(ios,Objective-C,类型信息查询)