【iOS_Development】isKindOfClass && isMemberOfClass
来源:互联网 发布:大神,我养你网络 番外 编辑:程序博客网 时间:2024/05/11 18:40
API文档
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)
异同点
相同点
都是NSObject的比较Class的方法
不同点
1. isKindOfClass来确定一个对象是否是一个类的成员,或者是派生自该类的成员2. isMemberOfClass只能确定一个对象是否是当前类的成员即isMemberOfClass不能检测任何的类都是基于NSObject类这一事实,而isKindOfClass可以
[[NSMutableData data] isKindOfClass:[NSData class]]; // YES [[NSMutableData data] isMemberOfClass:[NSData class]]; // NO 例: - (void)testIsKindOfClass { Class1 *c1=[[Class1 alloc] init]; if ([c1 isKindOfClass: [NSObject class]]) { NSLog(@"c1 is a kind of NSObject."); } if ([c1 isMemberOfClass:[Class1 class]]) { NSLog(@"c1 is Member of Class1 !"); } if (![c1 isMemberOfClass:[NSObject class]]) { NSLog(@"c1 is't Member of NSObject !"); } } 输出结果: c1 is a kind of NSObject . c1 is Member of Class1 ! c1 is't Member of NSObject !
特殊情况
苹果官方文档有这样一段描述
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 *myArr = [[NSArray alloc] init];NSMutableArray *myArr2 = [[NSMutableArray alloc] init];if ([myArr isKindOfClass:[NSArray class]]) { NSlog(@"myArr isKindOfClass of NSArray");}if ([myArr isMemberOfClass:[NSArray class]]) { NSlog(@"myArr isMemberOfClass of NSArray");}if ([myArr2 isKindOfClass:[NSMutableArray class]]) { NSlog(@"myArr2 isKindOfClass of NSMutableArray");}if ([myArr2 isMemberOfClass:[NSMutableArray class]]) { NSlog(@"myArr2 isMemberOfClass of NSMutableArray");}输出结果:myArr isKindOfClass of NSArraymyArr2 isKindOfClass of NSMutableArray
- 总结如下
- NSArray、NSMutableArray属于类簇,使用isMemberOfClass不能得到正确的结果
原因:由于类簇的性质,这类对象实际返回的实例有不确定性 - NSArray对象可能会在运行时发现其实运作的是NSCFArray(来自Core Foundation框架(C语言的实现版本),很多Cocoa对象都是如此做桥接的)
- 对于类簇的判断要谨慎
- NSArray、NSMutableArray属于类簇,使用isMemberOfClass不能得到正确的结果
- 【iOS_Development】isKindOfClass && isMemberOfClass
- isMemberOfClass/isKindOfClass
- isKindOfClass、isMemberOfClass
- iskindofclass or ismemberofclass
- isKindOfClass, isMemberOfClass, respondsToSelector, instancesRespondToSelector
- isKindOfClass, isMemberOfClass的区别
- isMemberOfClass 和 isKindOfClass
- isKindOfClass和isMemberOfClass
- isKindOfClass& isMemberOfClass respondsToSelector& instancesRespondToSelector
- isKindOfClass和isMemberOfClass
- isKindOfClass, isMemberOfClass的区别
- [iOS]isKindOfClass、isMemberOfClass、isSubclassOfClass
- isKindOfClass和 isMemberOfClass
- isKindOfClass和isMemberOfClass区别
- isKindOfClass, isMemberOfClass的区别
- isKindOfClass, isMemberOfClass的区别
- isMemberOfClass & isKindOfClass区别
- isKindOfClass,isSubclassOfClass ,isMemberOfClass 区别
- 用Python做测试——单用例调用
- annotation-config, annotation-driven, compont-scan 区别
- PHP 底层的运行机制与原理解析
- 不要脸的 S B
- linux挂载SD卡
- 【iOS_Development】isKindOfClass && isMemberOfClass
- 关于项目中依赖的design版本升级过后,项目中的自定义behavivor(上拉隐藏,下拉显示)的view隐藏后不再显示的问题解决方案
- Warning: Each child in an array or iterator should have a unique "key" prop. Check the render ...
- spring 实现事件的异步处理
- dp专题 第一题最长上升子序列
- Android 解决由于使用SlidingMenu导致虚拟键盘挡住底部菜单栏的问题
- AndroidStudio 使用SVN
- 运行Spark程序
- 遇到的问题笔记以及解决方案【长期更新】