久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

檢查對象是否為類類型

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?

如果答案還提供了一種方法來檢查數(shù)組中的項目是否為 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

推薦答案

要確定對象"是類還是實例,您需要檢查它是否是 元類在一個兩階段的過程中.首先調(diào)用 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 都有相同的結(jié)構(gòu)布局(Class isa),因此可以偽裝成對象,并且都可以接收消息很難確定哪個是哪個.這似乎是我能夠獲得一致結(jié)果的唯一方法.

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

這篇關(guān)于檢查對象是否為類類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中設置滾動視圖內(nèi)容大小)
Stop a UITableView from automatically scrolling(阻止 UITableView 自動滾動)
iOS UIScrollView Lazy Loading(iOS UIScrollView 延遲加載)
using iOS 6.0 SDK and building for iOS 5 Target causes UIScrollView setMinimumZoomScale to fail when running on iOS 5 simulator(在 iOS 5 模擬器上運行時,使用 iOS 6.0 SDK 并為 iOS 5 Target 構(gòu)建會導致 UIScrollView setMinimumZ
Create partial-screen UIPageViewController programmatically(以編程方式創(chuàng)建部分屏幕 UIPageViewController)
how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可縮放?)
主站蜘蛛池模板: 91资源在线观看 | av中文网| 欧美另类视频 | 国产目拍亚洲精品99久久精品 | 国产欧美在线一区 | 美女久久久久久久 | 91欧美激情一区二区三区成人 | 99精品视频一区二区三区 | 色综合99| 成年人免费在线视频 | 免费一区二区三区 | 中文字幕一区二区视频 | 在线免费看91 | 日韩一二区| 色一级 | 午夜精品一区二区三区在线观看 | 日韩欧美在线观看视频网站 | 欧美黑人狂野猛交老妇 | 成人欧美一区二区三区黑人孕妇 | 欧美成年网站 | 美日韩视频 | 欧美日韩黄 | 精品国产一区二区三区在线观看 | 色婷婷av一区二区三区软件 | 日韩精品中文字幕在线 | 成年网站在线观看 | 欧美日韩专区 | 自拍偷拍亚洲欧美 | 久久99网| 四虎永久免费黄色影片 | 在线日韩不卡 | 特黄小视频 | 日韩成人高清 | 91亚洲国产成人久久精品网站 | 欧美福利在线 | 成人影音| www.青青草 | 天天爽夜夜爽精品视频婷婷 | 日韩欧美在线观看 | 在线视频一区二区 | 一区二区三区中文字幕 |