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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With