Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if object is Class type

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

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         }     } 

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?

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

like image 797
Felipe Sabino Avatar asked Jun 30 '11 14:06

Felipe Sabino


People also ask

How do you know if an object is class?

Use the instanceof operator to check if an object is an instance of a class, e.g. if (myObj instanceof MyClass) {} . The instanceof operator checks if the prototype property of the constructor appears in the prototype chain of the object and returns true if it does.

How do you check if an object is a certain type of object?

You can check object type in Java by using the instanceof keyword. Determining object type is important if you're processing a collection such as an array that contains more than one type of object. For example, you might have an array with string and integer representations of numbers.

How do you check if an object is of a certain type Java?

The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .

How do you check the type of an object in Python?

To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.


1 Answers

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

like image 175
Joe Avatar answered Oct 07 '22 17:10

Joe