检查对象是否为类类型

时间:2023-01-03
本文介绍了检查对象是否为类类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个接收 Class 对象的 NSArray 的方法,我需要检查它们是否都是使用代码生成的 Class 类型如下:

I have a method that receives a NSArray of Class objects and I need to check if they all are Class type generated with the code bellow:

NSMutableArray *arr = [[NSMutableArray alloc] init];

[arr addObject:[NSObject class]];
[arr addObject:[NSValue class]];
[arr addObject:[NSNumber class]];
[arr addObject:[NSPredicate class]];
[arr addObject:@"not a class object"];

问题是Class不是objective-c类,是struc,所以不能随便用

The problem is that Class is not an objective-c class, it is a struc, so I can not use just

    for (int i; i<[arr count]; i++) {
        Class obj = [arr objectAtIndex:i];

        if([obj isKindOfClass: [Class class]]) {
            //do sth
        }
    }

所以,我需要检查 obj 变量是否是 Class 类型,我想它会直接在 C 中,但是我该怎么做?

So, I need to I check if the obj variable is a Class type, I suppose it will be in C directly, but how can I do that?

如果答案还提供了一种方法来检查数组中的项目是否为 NSObject,这将是一个加分项,如示例代码中的项目,NSPredicateNSObject 检查

It will be a plus if the answer also provide a way to check if the item in the array is a NSObject, as the items in the example code, the NSPredicate would also be true for the NSObject check

推荐答案

要确定对象"是类还是实例,您需要检查它是否是 元类在一个两阶段的过程中.首先调用 object_getClass 然后使用 class_isMetaClass.您将需要 #import <objc/runtime.h>.

To determine if an "object" is a class or an instance you need to check if it is a meta class in a two stage process. First call object_getClass then check if it is a meta class using class_isMetaClass. You will need to #import <objc/runtime.h>.

NSObject *object = [[NSObject alloc] init];
Class class = [NSObject class];

BOOL yup = class_isMetaClass(object_getClass(class));
BOOL nope = class_isMetaClass(object_getClass(object));

Class*id 都有相同的结构布局(Class isa),因此可以伪装成对象,并且都可以接收消息很难确定哪个是哪个.这似乎是我能够获得一致结果的唯一方法.

Both Class and *id have the same struct layout (Class isa), therefore can pose as objects and can both receive messages making it hard to determine which is which. This seems to be the only way I was able to get consistent results.

这是您的原始支票示例:

Here is your original example with the check:

NSMutableArray *arr = [[NSMutableArray alloc] init];

[arr addObject:[NSObject class]];
[arr addObject:[NSValue class]];
[arr addObject:[NSNumber class]];
[arr addObject:[NSPredicate class]];
[arr addObject:@"not a class object"];

for (int i; i<[arr count]; i++) {
    id obj = [arr objectAtIndex:i];

    if(class_isMetaClass(object_getClass(obj)))
    {
        //do sth
        NSLog(@"Class: %@", obj);
    }
    else
    {
        NSLog(@"Instance: %@", obj);
    }
}

[arr release];

还有输出:

类:NSObject
类:NSValue
类:NSNumber
类:NSPredicate
实例:不是类对象

Class: NSObject
Class: NSValue
Class: NSNumber
Class: NSPredicate
Instance: not a class object

这篇关于检查对象是否为类类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:uiscrollview 不会取消 uisegmentedcontrol 的内容触摸 下一篇:'CGFloat' 不能转换为 'UInt8' 和 Swift 和 Xcode 6 be

相关文章

最新文章