How to find out what class an NSObject belongs to

- by

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.



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.