Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between these inheritance checks?

Tags:

objective-c

I have seen examples of Objective-C code apparently doing the same/similar checks using different methods. I want to learn what's the difference in these:

isKindOfClass

isMemberOfClass

isSubclassOfClass

Please also mention if there is a check that I have not mentioned here, because I am learning and have a very poor command at things at the moment.

like image 848
Java Ka Baby Avatar asked Aug 28 '11 02:08

Java Ka Baby


2 Answers

These are both instance methods:

isKindOfClass: Is the object an instance of a class, or an instance of a subclass of that class? isMemberOfClass: Is the object is an instance of a class? (Does not include subclasses).

Whereas the last one is a class method, (e.g. [NSString isSubclassOfClass:[NSObject class]]).

isSubclassOfClass: Is a class equal to another class, or a subclass of that class?

like image 65
Chris Nolet Avatar answered Nov 15 '22 08:11

Chris Nolet


There are so many link on google and specially it has been asked on stackoverflow many times, check my added link in comment and,

iPhone SDK difference between isKindOfClass and isMemberOfClass

Try reading their documentation as well, that will help you a lot.

Edited

Lets say you have a class name External and inside External class you have a subclass named Internal. I hope you have idea about class can have a subclass which can access properties of a main class. so by this method you can ask an Internal class that , Are you a subclass of External class?

[Internal isSubclassOfClass:External]

And return value will be BOOL

  • YES if the receiving class is a subclass of—or identical to—aClass, otherwise NO.

More reading is available on documentation website of Apple

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

like image 37
TeaCupApp Avatar answered Nov 15 '22 06:11

TeaCupApp