types - In Objective-C, what is the equivalent of Java's "instanceof" keyword?

ID : 20158

viewed : 32

Tags : objective-ctypescastingequalitydowncastobjective-c

Top 5 Answer for types - In Objective-C, what is the equivalent of Java's "instanceof" keyword?

vote vote

97

Try [myObject class] for returning the class of an object.

You can make exact comparisons with:

if ([myObject class] == [MyClass class]) 

but not by using directly MyClass identifier.

Similarily, you can find if the object is of a subclass of your class with:

if ([myObject isKindOfClass:[AnObject class]]) 

as suggested by Jon Skeet and zoul.

vote vote

88

From Wikipedia:

In Objective-C, for example, both the generic Object and NSObject (in Cocoa/OpenStep) provide the method isMemberOfClass: which returns true if the argument to the method is an instance of the specified class. The method isKindOfClass: analogously returns true if the argument inherits from the specified class.

isKindOfClass: would be closest to instanceof, by the sounds of it.

vote vote

72

See the isKindOfClass: method in the NSObject documentation. (The usual word of warning for such question is that checking the object class is often a sign of doing something wrong.)

vote vote

62

vote vote

57

I have just found this pretty solution:

import sys; sys.path.insert(0, '..') # add parent folder path where lib folder is import lib.store_load # store_load is a file on my library folder 

You just want some functions of that file

from lib.store_load import your_function_name 

If python version >= 3.3 you do not need init.py file in the folder

Top 3 video Explaining types - In Objective-C, what is the equivalent of Java's "instanceof" keyword?

Related QUESTION?