Objective中的'isa'变量是否等于Java中的'instanceof' [英] Is the 'isa' variable in objective-C equal to 'instanceof' in Java

查看:152
本文介绍了Objective中的'isa'变量是否等于Java中的'instanceof'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题:可以公平地说,Objective-C中的'isa'实例变量提供与Java中'instanceof'运算符相同的功能吗?

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?

推荐答案

这些是不同的概念。

一个是结构的成员,而另一个是运算符

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

为了模仿Java中对 instanceof 运算符的严格解释,你可以在 isa 会员:

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
}

但是建议您使用 NSObject protocol的 -isMemberOfClass: 方法来完成此任务:

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
}

如果您有兴趣知道指定的类是ano的实例,还是ano的子类在类中,你应该使用 NSObject 协议的 -isKindOfClass:方法。

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.

这篇关于Objective中的'isa'变量是否等于Java中的'instanceof'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆