How to find out what class an NSObject belongs to

Sometimes you find yourself having a reference to an object (say a View Controller), but you don’t know what class it belongs to. Thankfully there’s a method for this which will work with any NSObject:

if ([self.navController isMemberOfClass:[UIViewController class]]) {
        self.previousTitle = @"Novels";
    }

In this example we’re testing if our self.navController is a UIViewController. The method returns a BOOL.

If you inherit from other classes and test for a parent class, rest assured that this does not return false positives. So if you’d test the above for being a member of NSObject, the equation would return NO.





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.