Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ooooh... fun question. The answer is a c-ism.</p> <p>Consider:</p> <pre><code>@interface MyClass : NSObject @end @implementation MyClass @end </code></pre> <p>Now, say you have:</p> <pre><code>... MyClass *m = nil; ... </code></pre> <p>In that context, the compiler sees <code>MyClass</code> as a type definition. The <code>*</code> says that the variable <code>m</code> is a <code>pointer to a hunk o' memory that contains one (or many -- don't forget your C pointer-fu) MyClass instances</code>.</p> <p>In other words, <code>MyClass</code> is a type.</p> <p>But, in the context of something like:</p> <pre><code>[someInstance isKindOfClass: x ]; </code></pre> <p><code>x</code> must be an rvalue or, in human terms, <em>the value of an expression</em>. A type, however, cannot be used as an rvalue.</p> <p>That <code>[MyClass class]</code> 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).</p> <p>And, as a matter of fact, you can do:</p> <pre><code>typedef MyClass Foo; .... [MyClass class]; [Foo Class]; </code></pre> <p>It'll all work. However, you can't do the following <em>but</em> the error message is illuminating:</p> <pre><code>[NSUInteger class]; </code></pre> <p><strong>error: ‘NSUInteger’ is not an Objective-C class name or alias</strong></p> <hr/> <p>Now, why not special case it everywhere as a bare name?</p> <p>That colludes type names and rvalues and you quickly end up having to swallow something like <code>[foo isKindOfClass: (MyClass)];</code> while barfing on <code>[foo isKindOfClass: (MyClass *)];</code> which then encroaches upon typecasting territory in a rather uncomfortable fashion.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload