本文介绍了isMemberOfClass的用法;返回假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我认为第二个条件为true,但事实证明它为false.我想念什么吗?请帮助我理解.

In the code below I thought the second condition would be true, but it is turning out as false. Am I missing something? Please help me understand.

NSArray *array = [[NSArray alloc] init];
NSLog(@"%@", NSStringFromClass([array class]));

if ([array isMemberOfClass:[NSObject class]]) {

    NSLog(@"Member NSObject"); //Didn't print; 
}

if ([array isMemberOfClass:[NSArray class]]) {

    NSLog(@"Member NSArray"); //Didn't print; I don't understand why?
}

if ([array isKindOfClass:[NSObject class]]) {

    NSLog(@"Kind of NSObject"); //Printed; Expected
}

if ([array isKindOfClass:[NSArray class]]) {

    NSLog(@"Kind of NSArray"); //Printed; Expected
}

修改

我将NSArray的子​​类创建为MyArray,并使用isMemberOfClass如下测试其实例

I created sub class of NSArray as MyArray and tested its instance using isMemberOfClass as below

if ([myArray isMemberOfClass:[MyArray class]]) {

    NSLog(@"Member MyArray"); //Printed;
}

所以,我猜想isMemberOfClass在NSArray上是不可能的,可能在其他一些框架类上也是如此.

So, I guess isMemberOfClass not possible on NSArray, probably on some other framework classes as well.

谢谢.

推荐答案

这是正确的行为.尝试检查该对象的实际类:

This is the correct behavior. Try inspecting the actual class for that object:

NSArray *array = [[NSArray alloc] init];
NSLog(@"%@", NSStringFromClass([array class]));

您得到的输出类似于:

2013-02-15 23:42:31.272 Project[91998:c07] __NSArrayI

因此,实际的类是__NSArrayI(NSArray的私有子类),而不是NSArray本身.通常,isKindOfClass:会提供更有用的结果.

So the actual class is __NSArrayI (a private subclass of NSArray), not NSArray itself. Typically, isKindOfClass: provides more useful results.

这篇关于isMemberOfClass的用法;返回假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 10:08