Under the Bridge

Tip: className

So you got some desktop Cocoa code to port that does some introspection, like

if ([[args className] isEqualToString:@"NSCFBoolean"])

and you noticed — whoops! — that -className doesn’t exist in the iPhone SDK, for no apparent reason other than to confuse you?

Well, no, neither did we until today. But if/when you do … here’s the trick to faking it yourself:

#import <objc/runtime.h>

- (NSString *)className
{
return [NSString stringWithUTF8String:class_getName([self class])];
}

Now you know!

3
  • jeremiah

    i think this would be preferable:

    [args isKindOfClass:[NSCFBoolean class]]

    and is supported on iphone.

  • http://www.alexcurylo.com/ Alex

    That’s fine if you have an actual object, yes.

    If you have a disconnected data store keyed on class name and no objects in sight, as opposed to a one line if statement on an existing object which is easy to put in a post, then is{Kind|Member}OfClass are inapplicable.

  • jeremiah

    ok, i see your point.

    yeah, there is lots of cool stuff in the runtime api.