sash
sash

Reputation: 8725

Check class or class instance

How to check if id is class definition or class instance? For example:

Class def =[NSString class];
NSString * inst= @"test";

[self check:def]; // should output "Class"
[self check:inst]; //should output "Instance"


-(void)check:(id)object
{
    if(objejct ... ){ // ???
        NSLog(@"Instance");
    }else{
        NSLog(@"Class");
    }
}

Upvotes: 1

Views: 129

Answers (1)

muffe
muffe

Reputation: 2295

You need to

#import <objc/runtime.h>

then your check function needs to look like this

-(void)check:(id)object
{
    if(class_isMetaClass(object_getClass(object)))
    {
        NSLog(@"Class");
    }
    else
    {
        NSLog(@"Instance");
    }
}

Upvotes: 3

Related Questions