Skip to content

iOS isKindOfClass

Platform License

In Cocoa, almost all classes inherit from NSObject.

That means that NSObject is ‘the superclass’, and (almost) the rest of the classes are subclasses of NSObject.

Sample

isKindOfClass:

Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.

if (element is UIImageView) {

}
or

let viewControllers: [UIViewController] = (self.navigationController?.viewControllers)! as [UIViewController];
    for aViewController:UIViewController in viewControllers {
        if aViewController.isKind(of: TestViewController.self) {
            print(aViewController)
            return //return for stop the loop
        }
    }
if ([element isKindOfClass: [UIImageView class]]) {

}

or

NSArray *viewControllers = [self.navigationController viewControllers];
    for (id aViewController in viewControllers) {
        if([aViewController isKindOfClass:[TestViewController class]]){
            print(aViewController)
            return;//return for stop the loop
        }
    }

Let's grow together 🌱

Cheers 🍻