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
,这将是一个加分项,如示例代码中的项目,NSPredicate
NSObject 检查
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
这篇关于检查对象是否为类类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检查对象是否为类类型
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01