Check if object is Class type

后端 未结 5 1903

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

相关标签:
5条回答
  • 2020-12-02 10:26

    Update: In iOS 8+ or OS X 10.10+, you can just do:

    object_isClass(obj)
    

    (You will need to #import <objc/runtime.h>.)

    0 讨论(0)
  • 2020-12-02 10:33

    Joe’s answer is good. A simple alternative that works in most situations is to check if the object returns itself in response to class.

    if ([obj class] == obj) { … }
    

    This works because the NSObject meta-class overrides class and returns the class object (itself) — see the NSObject Class Reference. This does not require the runtime headers, but assumes the objects are subclasses of NSObject and don’t override -class or +class and do something unusual.


    With the input in the question, the results are the same as Joe’s:

    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 (id<NSObject> obj in arr) {
    
        if ([obj class] == obj) {
            NSLog(@"Class: %@", obj);
        }
        else {
            NSLog(@"Instance: %@", obj);
        }
    }
    

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

    0 讨论(0)
  • 2020-12-02 10:47

    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));
    

    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.

    EDIT:

    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];
    

    And the output:

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

    0 讨论(0)
  • 2020-12-02 10:49

    No problem here is how you do it:

    if([NSStringFromClass([obj class]) isEqualToString:@"Class"]){
        NSLog(@"It is type of Class");
    }
    

    Edit

    Or you can make your Class conform to a protocol: and check if obj that you get from the array conforms to this protocol like this:

    if([obj conformsToProtocol:@protocol(MyClassProtocol)])
    

    Edit

    Or you can check if what you get from the array is an NSObject complient

    if ([object conformsToProtocol:@protocol(NSObject)]) {
        // Do something
    }
    
    0 讨论(0)
  • 2020-12-02 10:50

    If you need to verify if the object in the array is a Class object then you can verify if it responds to class methods.

    for ( id obj in arr ) {
        if (([obj respondsToSelector:@selector(isSubclassOfClass:)])
              && (obj == [NSObject class]) ) {
            NSLog(@"%@", obj);
        }
    }
    

    Once you know it is a Class object by verifying if it responds to isSubclassOfClass: then you can check for direct equality with [NSObject class].

    0 讨论(0)
提交回复
热议问题