检查对象是否为类类型

Check if object is Class type(检查对象是否为类类型)
本文介绍了检查对象是否为类类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个接收 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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

IncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出现 IncompatibleClassChangeError)
Error quot;File google-services.json is missing from module root folder. The Google Services Plugin cannot function without itquot;(错误“模块根文件夹中缺少文件 google-services.json.没有它,Google 服务插件将无法运行) - IT屋-程序员软件开发技
com.google.android.gms:play-services-measurement-base is being requested by various other libraries(com.google.android.gms:play-services-measurement-base 正在被其他各种库请求)
Error:Execution failed for task #39;:android:transformClassesAndResourcesWithProguardForRelease#39;(错误:任务:android:transformClassesAndResourcesWithProguardForRelease的执行失败)
Error:Execution failed for task #39;:app:transformClassesWithDexForDebug#39;(错误:任务“:app:transformClassesWithDexForDebug执行失败)
Failed to resolve: com.google.firebase:firebase-core:11.2.0(无法解决:com.google.firebase:firebase-core:11.2.0)