[英]Why does class method of a class object does not return the meta class?
The following code:以下代码:
NSObject *a = [[NSObject alloc] init];
Class c = [a class];
Class meta1 = [[a class] class];
Class meta2 = objc_getMetaClass("NSObject");
NSLog(@"%@ %d", c, class_isMetaClass(c));
NSLog(@"%@ %d", meta1, class_isMetaClass(meta1));
NSLog(@"%@ %d", meta2, class_isMetaClass(meta2));
produce:生产:
NSObject 0
NSObject 0
NSObject 1
More than that [c isKindOfClass:c]
returns true
.不仅如此, [c isKindOfClass:c]
返回true
。
meta1
is the result of calling class
on a value of type Class
, ie the calls invokes a class method ( + class
) – similarly the call [c alloc]
(after the assignment to c
in the code) would be equivalent to [NSObject alloc]
. meta1
是对Class
类型的值调用class
的结果,即调用调用类方法( + class
)——类似地,调用[c alloc]
(在代码中赋值给c
之后)将等效于[NSObject alloc]
。
The default implementation of + class
comes from NSObject
and is defined to simply return the class it was called on. + class
的默认实现来自NSObject
并被定义为简单地返回它被调用的类。 So in the example code both c
and meta1
have as value NSObject
's Class
.所以在示例代码中, c
和meta1
值都是NSObject
的Class
。
That tells you why-by-definition meta1
is not the meta class.这告诉你为什么按定义meta1
不是元类。 If your question is why-by-design does it not return the meta class then that is a different question best asked of one of the language's designers!如果您的问题是为什么按设计不返回元类,那么最好向该语言的设计者之一提出不同的问题!
HTH HTH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.