160

What is the difference between the isKind(of aClass: AnyClass) and the isMember(of aClass: AnyClass) functions in Swift?

Original Question in Objective-C

What is the difference between the isKindOfClass:(Class)aClass and the isMemberOfClass:(Class)aClass functions? I know it is something small like, one is global while the other is an exact class match but I need someone to specify which is which please.

rolling_codes
  • 15,174
  • 22
  • 76
  • 112

6 Answers6

275

isKindOfClass: returns YES if the receiver is an instance of the specified class or an instance of any class that inherits from the specified class.

isMemberOfClass: returns YES if, and only if, the receiver is an instance of the specified class.

Most of the time you want to use isKindOfClass: to ensure that your code also works with subclasses.

The NSObject Protocol Reference talks a little more about these methods.

karlbsm
  • 347
  • 4
  • 9
Sebastian Celis
  • 12,185
  • 6
  • 36
  • 44
  • Can you please clear my below doubt? if ([lbl.textColor isMemberOfClass:[UIColor class]]) { // Not Memeber NSLog(@"Not Memeber"); }else { NSLog(@"Not Memeber"); } if ([imgView.image isMemberOfClass:[UIImage class]]) {// Memeber NSLog(@"Memeber"); }else { NSLog(@"Not Memeber"); } – Nikkie Dec 19 '14 at 14:39
84
  • isKindOfClass: indicates whether an object inherits from a given class
  • isMemberOfClass: indicates whether an object is an instance of a given class.

[[NSMutableData data] isKindOfClass:[NSData class]]; // YES
[[NSMutableData data] isMemberOfClass:[NSData class]]; // NO
jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • 1
    isKindOfClass will also return YES if the object is an instance of a given class. In other words isMemberOfClass is a subset of isKindOfClass. – Scooter Aug 12 '18 at 14:31
48

Suppose

@interface A : NSObject 
@end

@interface B : A
@end

...

id b = [[B alloc] init];

then

[b isKindOfClass:[A class]] == YES;
[b isMemberOfClass:[A class]] == NO;

Basically, -isMemberOfClass: is true if the instance is exactly of the specified class, while -isKindOfClass: is true if the instance is exactly of the specified class or if one of the instance's ancestors is of the specified class.

-isMemberOfClass: is seldom used.

ma11hew28
  • 121,420
  • 116
  • 450
  • 651
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 2
    But if you have an array of subviews which include UIViews and a UISegmentedControl and you looped through them and set a conditional on class you would need to use isMemberOfClas UIView and isMemberOfClass UISegmentedControl to distinguish between them, no? isKindOfClass would see the UISegmentedControl as a UIView. – PruitIgoe Aug 01 '13 at 18:36
  • 1
    @Pruitlgoe that is very true. You might use isKindOfClass:[UIView class] to ensure that all objects you are dealing with are UIViews but you would need to use isMemberOfClass:[UIView class] and/or isMemberOfClass:[UISegmentedControl class] inside some conditional statement to indicate any distinct implementation of the views based on their immediate instance class – rolling_codes Jul 02 '14 at 13:48
9

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.

isMemberOfClass: Returns a Boolean value that indicates whether the receiver is an instance of a given class.

Community
  • 1
  • 1
Alex Terente
  • 12,006
  • 5
  • 51
  • 71
2

isKindOfClass-> return YES when the object is instance of that class or instance of a class which is inherited from it.

isMemberOfClass: return YES when the object is instance of that class but No in case: instance of a class which is inherited from it.

example is good enough in jtbandes answer.

Ishu
  • 12,797
  • 5
  • 35
  • 51
0

Because of class clusters, isMemberOfClass can give you an answer you might not expect. In many cases your best choice is more likely to be -(BOOL)conformsToProtocol:(SEL)aSelector or - (BOOL)conformsToProtocol:(Protocol*)aProtocol. I.e, it's better to test these if they can answer your need rather than testing class/subclass.

See apple doc for NSObject class and protocol:

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/cl/NSObject

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intf/NSObject

Art Swri
  • 2,799
  • 3
  • 25
  • 36