Why do we have to do [MyClass class] in Objective-C?

后端 未结 8 1626
一向
一向 2020-12-13 00:01

In Objective-C, you can invoke class methods with:

[MyClass aClassMethod];

And you can query an instance\'s kind with:

[som         


        
8条回答
  •  悲&欢浪女
    2020-12-13 00:30

    Interesting.

    In Objective-C, class name has two roles, as a data type and as a class object. As a data type name, you can do things like:

    MyClass *anObject;
    

    As a class object, the class name can stand for the class object only as a message receiver. And this is why you have to use

    ... isKindOfClass:[MyClass class] ...
    

    However, I don't think this is the answer which can satisfy your need. To me, the answer is, "yes, what you want is plausible. But the spec says the other way".

    Reference: The Objective-C Programming Language Page 32, section: "Class Names in Source Code".

提交回复
热议问题