views:

583

answers:

1

Let's say I have a generic pointer in objective-c. This pointer could either be a Class object, or it could be an instance of that class. Is there any way to tell the difference between the two?

Example:


id pointerA = [someClass class];
id pointerB = [[someClass alloc] init];

bool pointerAIsAClass = is_this_a_Class(pointerA); // should be true
bool pointerBIsAClass = is_this_a_Class(pointerB); // should be false

How would I write the is_this_a_Class function so that it returns the proper bool value?

bool is_this_a_Class(id somePointer)
{
    // your code goes here
}
+3  A: 

I don't see a better way, and this isn't foolproof, but this should work:

BOOL pointer_isClass(id object) {
    return [object respondsToSelector:@selector(instancesRespondToSelector:)];
}

Since, theoretically, only Class objects would respond to the -instancesRespondToSelector: message this should work. Outside of an actual objc_* call though I don't see any real API for this.

UPDATE:
After reading Giao's answer another thought came to me, test the pointer's response to the -class method. Calling -class on a class object should be equivalent to calling -self on an object instance but would return another object on an object instance.

BOOL pointer_isClass(id object) {
    return object == [object class];
}

I think this should be more foolproof, especially in the case where an object instance implements -instancesRespondToSelector: this one should work 100% of the time.

Ashley Clark
Yep, that works. Thank you!
e.James
Beautiful. Your new method works and is much more elegant. Thanks again :)
e.James
There's a bunch of useful information about this type of thing here:http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html
Jon Hess