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.





Leave a Reply