Parmanoir

Telling classes from instances

In ObjC, how do you know the kind of object you're handling ? Classes still seem a little mysterious to me as they're not defined as objects but behave like objects : you can call [object method] whether object is a class or an instance, and the runtime will automatically call class (+) or instance (-) methods. If comes the time where you need to tell them apart, a simple way is to check if an object matches its class :
// A class
id object1 = NSClassFromString(@"NSApplication");
// An instance
id object2 = [object1 sharedApplication];

// Telling instances from classes …

// Evaluates to YES
isClass = object1 == [object1 class];

// Evaluates to NO
isClass = object2 == [object2 class];
Or did I miss some obvious "already there" method ?

There is very rarely a need to know your class.

There are two places I know of where you (sort of) need to know a class.

1) You're working on backwards compatibility, some methods are only supported on newer versions of the OS. Use respondsToSelector: to figure out if your object is OK

2) You want to verify your object can behave a certain way. All you need to determine is if it conforms to a specific protocol - use conformsToProtocol:

That way, any implementation that satisfies those less stringent requirements will still work - you're not tied to your original choice of class.

Karsten
2008 07 10

why not implement a method: +isClass and -isClass or +isInstance and -isInstance in Object?

nothing to compare then, just return a YES and a NO in the right methods ;-)

Karsten

Dmitry Chestnykh
2008 07 11

First:

BOOL isClass = ![object1 isMemberOfClass:[object1 class]];

Seems like class is not a member of itself ;)... However docs say:

"Class objects may be compiler-created objects but they still support
the concept  of membership. Thus, you can use this method to verify
that the receiver is a specific Class object."

Second, total hack:

BOOL isClass = (NSStringFromClass(object1) != nil);
Patrick Geiller
2008 07 11

@Robert I'm writing a bridge from JavascriptCore to Cocoa and I need to know what kind of object the interpreter is giving me back. Agreed, that's extremely useless in everyday programming but that makes a nice blog post !

@Karsten You get my vote for pragmatic programming :)

@Dmitry Thanks for the hack ! So there is no official method. Frustrating since NSStringFromClass seems to be using the unofficial one …

Ah. In hindsight, I misread your article :) Thanks for clarifying

player2
2008 07 12

Classes are objects. They are of class Class. As mentioned before, you rarely need to actually know to what class an object belongs; just ask it -respondsToSelector: or -conformsToProtocol:. But if you really need to know if you're dealing with an object of a specific class Foo, just ask [obj isMemberOfClass:[Foo class]]. Putting two and two together, if you want to know if a specific object obj is actually a class, ask [obj isMemberOfClass:[Class class]].

player2
2008 07 12

...and I seem to have suffered from the same misreading. Sorry.

Patrick Geiller
2008 07 12

If classes are objects, where are they defined ? I didn't find

@interface Class

in the headers. That's what I don't get about classes : they do behave like objects but Class is defined as a struct !?


Follow me on Twitter
Planet Cocoa
Cocoa.fr

2011 02 22Distance field
2010 07 202Binding through NSApp
2010 05 122Forwarding invocations
2010 02 272Core Image black fringes
2010 02 21Quickest Way to Shell
2010 02 08Who's calling ?
2009 09 2138 ways to use Blocks in Snow Leopard
2009 08 182Bracket Mess
2009 08 124Taming JavascriptCore within and without WebView
2009 04 15Debugging with Activity Monitor
2009 03 25How Core Image Color Tracking works
2009 03 1510Custom NSThemeFrame
2009 03 10Which framework is running ?
2009 03 074CoreUI can paint pretty big
2009 02 18Localization with functions
2009 01 30Did you forget to nest alloc and init?
2009 01 16JSCocoa on the iPhone
2009 01 11Mixing WebView and JavascriptCore
2009 01 09Badge overflow
2009 01 09Find your Garbage Collection leaks with Instruments

Powered by MediaWiki