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

后端 未结 8 1498
一向
一向 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:24

    I'm thinking that MyClass is actually a meta-class. You send it the class message to get the actual class (of type Class).

    0 讨论(0)
  • 2020-12-13 00:28

    Ooooh... fun question. The answer is a c-ism.

    Consider:

    @interface MyClass : NSObject
    @end
    @implementation MyClass
    @end
    

    Now, say you have:

    ...
    MyClass *m = nil;
    ...
    

    In that context, the compiler sees MyClass as a type definition. The * says that the variable m is a pointer to a hunk o' memory that contains one (or many -- don't forget your C pointer-fu) MyClass instances.

    In other words, MyClass is a type.

    But, in the context of something like:

    [someInstance isKindOfClass: x ];
    

    x must be an rvalue or, in human terms, the value of an expression. A type, however, cannot be used as an rvalue.

    That [MyClass class] works is actually a bit of a hack, both in the language and the compiler in that the grammar specifically allows a type name to be the message receiver (to be the target of a method call).

    And, as a matter of fact, you can do:

    typedef MyClass Foo;
    ....
    [MyClass class];
    [Foo Class];
    

    It'll all work. However, you can't do the following but the error message is illuminating:

    [NSUInteger class];
    

    error: ‘NSUInteger’ is not an Objective-C class name or alias


    Now, why not special case it everywhere as a bare name?

    That colludes type names and rvalues and you quickly end up having to swallow something like [foo isKindOfClass: (MyClass)]; while barfing on [foo isKindOfClass: (MyClass *)]; which then encroaches upon typecasting territory in a rather uncomfortable fashion.

    0 讨论(0)
  • 2020-12-13 00:29

    @John and @ryanprayogo -- you are both fundamentally wrong. MyClass is a Class, which is also an object, but does not inherit from NSObject. Objective-C is kind of weird this way, but its actually brilliant when fully explained (See here). The answer here, though, is as @yehnan said, that a class name can be either a type name for declarators and casts, or as a receiver for messages. The implementation of [MyClass class] returns self (which is, within the method, MyClass). Also as @yehnan said, the language could support passing it as an argument, although it simply doesn't.

    0 讨论(0)
  • 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".

    0 讨论(0)
  • 2020-12-13 00:42

    My first glance answer is because [MyClass class] return a object of type Class, and MyClass doesn't inherit from Class...

    0 讨论(0)
  • 2020-12-13 00:45

    Because what the isKindOfClass expects is a "class" and that's what get returned from invoking: [MyClass class]

    0 讨论(0)
提交回复
热议问题