iOS difference between isKindOfClass and isMemberOfClass

IosObjective CSwiftClassInheritance

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

  • 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

Solution 3 - Ios

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.

Solution 4 - Ios

> 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.

Solution 5 - Ios

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.

Solution 6 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNoodleOfDeathView Question on Stackoverflow
Solution 1 - IosSebastian CelisView Answer on Stackoverflow
Solution 2 - IosjtbandesView Answer on Stackoverflow
Solution 3 - IoskennytmView Answer on Stackoverflow
Solution 4 - IosAlex TerenteView Answer on Stackoverflow
Solution 5 - IosIshuView Answer on Stackoverflow
Solution 6 - IosArt SwriView Answer on Stackoverflow