OC中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. (required)
    这个方法用来判断一个对象是否是指定类或者某个从该类继承类的实例对象。

  • isMemberOfClass:
    Returns a Boolean value that indicates whether the receiver is an instance of a given class. (required)
    这个方法用来判断一个对象是否是指定类的实例对象。

两者区别:
  • isKindOfClass 可以判断某对象是否是某个类的实例对象,这个类和这个类的继承类都可以判断;
  • isMemberOfClass只能判断对象是否是当前类的实例对象。
isMemberOfClass和isKindOfClass的应用举例
UIScrollView *scrollView = [[UIScrollView alloc] init];
if ([scrollView isKindOfClass:[UIView class]]) {
    NSLog(@"scrollView is isKindOfClass UIView");
}

if ([scrollView isKindOfClass:[UIScrollView class]]) {
    NSLog(@"scrollView is isKindOfClass UIScrollView");
}

if ([scrollView isMemberOfClass:[UIView class]]) {
    NSLog(@"scrollView is isMemberOfClass UIView");
}

if ([scrollView isMemberOfClass:[UIScrollView class]]) {
    NSLog(@"scrollView isMemberOfClass UIScrollView");
}

输出结果:
scrollView is isKindOfClass UIView
scrollView is isKindOfClass UIScrollView
scrollView isMemberOfClass UIScrollView

另外需要特别注意的是NSArray、NSMutableArray这样的类:苹果官方文档有这样一段描述

Be careful when using this method on objects represented by a class cluster. Because of the nature of class clusters, the object you get back may not always be the type you expected. If you call a method that returns a class cluster, the exact type returned by the method is the best indicator of what you can do with that object. For example, if a method returns a pointer to an NSArray object, you should not use this method to see if the array is mutable, as shown in the following code:

// DO NOT DO THIS!
if ([myArray isKindOfClass:[NSMutableArray class]])
{
    // Modify the object
}

If you use such constructs in your code, you might think it is alright to modify an object that in reality should not be modified. Doing so might then create problems for other code that expected the object to remain unchanged.

If the receiver is a class object, this method returns YES if aClass is a Class object of the same type, NO otherwise.

测试结果:

NSArray *testArray = [[NSArray alloc] init];
NSMutableArray *testArray2 = [[NSMutableArray alloc] init];
if ([testArray isKindOfClass:[NSArray class]]) {
    NSLog(@"testArray isKindOfClass of NSArray");
}

if ([testArray isMemberOfClass:[NSArray class]]) {
    NSLog(@"testArray isMemberOfClass of NSArray");
}

if ([testArray2 isKindOfClass:[NSMutableArray class]]) {
    NSLog(@"testArray2 isKindOfClass of NSMutableArray");
}

if ([testArray2 isMemberOfClass:[NSMutableArray class]]) {
    NSLog(@"testArray2 isMemberOfClass of NSMutableArray");
}

控制台输出:
testArray isKindOfClass of NSArray
testArray2 isKindOfClass of NSMutableArray

各种查询得出的结论是:NSArray、NSMutableArray属于类簇,使用isMemberOfClass不能取到正确的结果。

原因是:由于类簇的性质,这类对象实际返回的实例有不确定性。
NSArray对象可能会在运行时发现其实运作的是NSCFArray(来自Core Foundation框架(C语言的实现版本),很多Cocoa对象都是如此做桥接的)
总之对于类簇的判断要谨慎。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值