Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the 'isa' variable in objective-C equal to 'instanceof' in Java

Simple question: is it fair to say that the 'isa' instance variable in Objective-C provides the same functionality as the 'instanceof' operator in Java?

like image 241
2bard Avatar asked Nov 28 '11 09:11

2bard


1 Answers

These are different concepts.

One is a member of a struct, while another is an operator.

To mimic a strict interpretation of the instanceof operator in Java, you can do pointer comparison on the isa member:

if(obj->isa == [SomeClass class]) {
  //obj is an instance of SomeClass
}

But it is recommended that you use the NSObject protocol's -isMemberOfClass: method to accomplish this instead:

if([obj isMemberOfClass:[SomeClass class]]) {
  //obj is an instance of SomeClass
}

If you are interested in knowing if the specified class is an instance of, or is a subclass of another class, you should use the NSObject protocol's -isKindOfClass: method.

like image 166
Jacob Relkin Avatar answered Nov 15 '22 18:11

Jacob Relkin