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?
简单的问题:可以公平地说,Objective-C中的'isa'实例变量提供与Java中'instanceof'运算符相同的功能吗?
4 个解决方案
#1
14
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:
要模仿Java中instanceof运算符的严格解释,可以在isa成员上进行指针比较:
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:
但建议您使用NSObject协议的-isMemberOfClass:方法来完成此操作:
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.
如果您有兴趣知道指定的类是否是另一个类的实例,或者是另一个类的子类,则应使用NSObject协议的-isKindOfClass:方法。
#2
4
From the Objective-C programming guide:
来自Objective-C编程指南:
id is defined as pointer to an object data structure:
id被定义为指向对象数据结构的指针:
typedef struct objc_object {
Class isa;
} *id;
Every object thus has an isa variable that tells it of what class it is an instance. Since the Class type is itself defined as a pointer:
因此,每个对象都有一个isa变量,告诉它它是一个实例的类。由于Class类型本身被定义为指针:
typedef struct objc_class *Class;
the isa variable is frequently referred to as the “isa pointer.”
isa变量通常被称为“isa指针”。
#3
3
No. instanceof
is more like the isKindOfClass:
method in Objective-C because it also evaluates to true
for subclasses while the isa
pointer only points to a single class.
numberof更像是Objective-C中的isKindOfClass:方法,因为它也为子类求值为true,而isa指针只指向一个类。
#4
0
In 64-bit implementations of the Objective-C runtime, isa became 64-bits - like all other pointers.
在Objective-C运行时的64位实现中,isa变为64位 - 就像所有其他指针一样。
Apple is using a clever trick to speed things up: they are relying on the fact that all 64-bits aren't used for the address, so they are using some of the bits to store things like retain count. This way they don't need to go modify the retain count for an object in a separate table with all the performance implications that that entails.
Apple正在使用一个聪明的技巧来加快速度:它们依赖于所有64位都不用于地址的事实,因此它们使用一些位来存储诸如保留计数之类的东西。这样,他们就不需要在单独的表中修改对象的保留计数,并具有所需的所有性能影响。
What this means is that direct comparison of the isa pointer does not work at all. This is true for OS X and now iOS 7 on the 64-bit A7 (iPhone 5s).
这意味着直接比较isa指针根本不起作用。 OS X和现在的iOS 7在64位A7(iPhone 5s)上都是如此。
Treat isa as an implementation detail. Do not access it directly. Use -isMemberOfClass: which will do the proper thing (which in 64-bit OSes now involves masking off part of isa).
将isa视为实现细节。不要直接访问它。使用-isMemberOfClass:这将做正确的事情(在64位操作系统现在涉及屏蔽部分isa)。
More info: http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html
更多信息:http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html
#1
14
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:
要模仿Java中instanceof运算符的严格解释,可以在isa成员上进行指针比较:
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:
但建议您使用NSObject协议的-isMemberOfClass:方法来完成此操作:
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.
如果您有兴趣知道指定的类是否是另一个类的实例,或者是另一个类的子类,则应使用NSObject协议的-isKindOfClass:方法。
#2
4
From the Objective-C programming guide:
来自Objective-C编程指南:
id is defined as pointer to an object data structure:
id被定义为指向对象数据结构的指针:
typedef struct objc_object {
Class isa;
} *id;
Every object thus has an isa variable that tells it of what class it is an instance. Since the Class type is itself defined as a pointer:
因此,每个对象都有一个isa变量,告诉它它是一个实例的类。由于Class类型本身被定义为指针:
typedef struct objc_class *Class;
the isa variable is frequently referred to as the “isa pointer.”
isa变量通常被称为“isa指针”。
#3
3
No. instanceof
is more like the isKindOfClass:
method in Objective-C because it also evaluates to true
for subclasses while the isa
pointer only points to a single class.
numberof更像是Objective-C中的isKindOfClass:方法,因为它也为子类求值为true,而isa指针只指向一个类。
#4
0
In 64-bit implementations of the Objective-C runtime, isa became 64-bits - like all other pointers.
在Objective-C运行时的64位实现中,isa变为64位 - 就像所有其他指针一样。
Apple is using a clever trick to speed things up: they are relying on the fact that all 64-bits aren't used for the address, so they are using some of the bits to store things like retain count. This way they don't need to go modify the retain count for an object in a separate table with all the performance implications that that entails.
Apple正在使用一个聪明的技巧来加快速度:它们依赖于所有64位都不用于地址的事实,因此它们使用一些位来存储诸如保留计数之类的东西。这样,他们就不需要在单独的表中修改对象的保留计数,并具有所需的所有性能影响。
What this means is that direct comparison of the isa pointer does not work at all. This is true for OS X and now iOS 7 on the 64-bit A7 (iPhone 5s).
这意味着直接比较isa指针根本不起作用。 OS X和现在的iOS 7在64位A7(iPhone 5s)上都是如此。
Treat isa as an implementation detail. Do not access it directly. Use -isMemberOfClass: which will do the proper thing (which in 64-bit OSes now involves masking off part of isa).
将isa视为实现细节。不要直接访问它。使用-isMemberOfClass:这将做正确的事情(在64位操作系统现在涉及屏蔽部分isa)。
More info: http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html
更多信息:http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html