iOS
isKindOfClass
isMemberOfClass
Objective-C
programming

iOS difference between isKindOfClass and isMemberOfClass

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

isKindOfClass: and isMemberOfClass: both answer type questions in Objective-C, but they answer different questions. One checks whether an object is an instance of a class or any subclass. The other checks whether the object is an instance of exactly that class and nothing more derived.

isKindOfClass: Includes Subclasses

Use isKindOfClass: when you care about the inheritance family. If the object is an instance of the requested class or any subclass, the result is YES.

objective-c
1#import <Foundation/Foundation.h>
2
3@interface Animal : NSObject
4@end
5
6@implementation Animal
7@end
8
9@interface Dog : Animal
10@end
11
12@implementation Dog
13@end
14
15int main(int argc, const char * argv[]) {
16    @autoreleasepool {
17        Dog *dog = [Dog new];
18
19        NSLog(@"%@", [dog isKindOfClass:[Animal class]] ? @"YES" : @"NO");
20        NSLog(@"%@", [dog isKindOfClass:[Dog class]] ? @"YES" : @"NO");
21    }
22    return 0;
23}

Both calls return YES because a Dog object is also an Animal in inheritance terms.

isMemberOfClass: Requires an Exact Match

Use isMemberOfClass: when you need to know whether the runtime class is exactly the specified class.

objective-c
1#import <Foundation/Foundation.h>
2
3@interface Animal : NSObject
4@end
5
6@implementation Animal
7@end
8
9@interface Dog : Animal
10@end
11
12@implementation Dog
13@end
14
15int main(int argc, const char * argv[]) {
16    @autoreleasepool {
17        Dog *dog = [Dog new];
18
19        NSLog(@"%@", [dog isMemberOfClass:[Animal class]] ? @"YES" : @"NO");
20        NSLog(@"%@", [dog isMemberOfClass:[Dog class]] ? @"YES" : @"NO");
21    }
22    return 0;
23}

The first call returns NO, and the second returns YES. That is the entire behavioral difference in one example.

Choose Based on Intent

In most application code, isKindOfClass: is what you want. If a method accepts a UIView, it usually should also accept subclasses such as UILabel, UIButton, or a custom view. Rejecting subclasses with isMemberOfClass: would be unnecessarily strict and would often violate polymorphism.

isMemberOfClass: is more appropriate when exact type identity matters, such as:

  • Special-case debugging or diagnostics.
  • Framework internals that behave differently for a concrete class.
  • Defensive checks around class clusters or proxy objects where subclass behavior is intentionally different.

Even then, exact-class checks should be used sparingly. If subclass behavior is valid, isKindOfClass: is the more flexible and usually more correct choice.

That rule of thumb covers most UIKit work: if the question is “can I treat this object as this kind of thing,” use isKindOfClass:. If the question is “is this exactly this runtime class,” use isMemberOfClass:.

UIKit Example

Suppose you walk through subviews and want to perform setup for any text input control. If you check only exact membership, you can accidentally exclude subclasses and custom controls.

objective-c
1for (UIView *view in self.view.subviews) {
2    if ([view isKindOfClass:[UITextField class]]) {
3        UITextField *textField = (UITextField *)view;
4        textField.textColor = [UIColor systemBlueColor];
5    }
6}

That code works for UITextField and subclasses. Switching to isMemberOfClass: would skip subclassed text fields, which is usually not what you want in UI code.

Be Aware of Class Objects Versus Instances

These methods are instance methods. They answer questions about objects, not class declarations themselves. If you need to compare class objects, you may instead use identity checks or methods such as isSubclassOfClass: on the class object. Mixing those concepts is a common source of confusion when working with Objective-C runtime APIs.

This confusion shows up often in debugging because developers print a class object, then mentally compare that to an instance check. The runtime APIs look similar, but they answer different questions.

Common Pitfalls

  • Using isMemberOfClass: when subclasses should also be accepted.
  • Assuming both methods behave the same for inherited classes.
  • Forgetting that these are instance checks, not general class comparison tools.
  • Overusing exact-type checks and making code less polymorphic.
  • Missing custom subclasses in UIKit because the check is too strict.

Summary

  • 'isKindOfClass: returns YES for the specified class and its subclasses.'
  • 'isMemberOfClass: returns YES only for the exact runtime class.'
  • In most app code, isKindOfClass: is the more appropriate choice.
  • Use isMemberOfClass: only when exact type identity truly matters.
  • Keep instance checks separate from class-object comparison logic.

Course illustration
Course illustration

All Rights Reserved.