Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I consider this case as being described in the link you provided:</p> <blockquote> <p>Boxing a value of enum type will result in a NSNumber pointer with a creation method according to the underlying type of the enum, which can be a fixed underlying type or a compiler-defined integer type capable of representing the values of all the members of the enumeration:</p> <pre><code>typedef enum : unsigned char { Red, Green, Blue } Color; Color col =&gt; Red; NSNumber *nsCol = @(col); // =&gt; [NSNumber numberWithUnsignedChar:] </code></pre> </blockquote> <p>but the details of the promotions in the libraries are not covered, and that is where the difference in expectations is introduced.</p> <p><code>-testA</code> ends up calling <code>+[NSNumber numberWithInt:]</code></p> <p><code>-testB</code> ends up calling <code>+[NSNumber numberWithUnsignedInt:]</code></p> <p>So the abstracted 'promotion' you see is because <code>CFNumber</code> (and consequently <code>NSNumber</code>) do not actually support unsigned values at this time (see constants of <code>CFNumberType</code> enums) -- based on the output you are seeing, one would then assume <code>NSNumber</code> implementations simply promote to the next signed type capable of representing all values in the case of an unsigned initializer of constructor -- apparently without testing the value to see if any 'width minimization' can be applied.</p> <p>Of course, <code>NSNumber</code> declares constructors and initializers which take unsigned types as parameters, but the internal representation of an unsigned integer is actually stored as a signed integer representation.</p> <p>The compiler appears to call appropriate/exact convenience constructors when promoting the boxed literal to an <code>NSNumber</code>. For example a <code>uint16_t</code> typed enum will be stored as a 32 bit int (via <code>numberWithUnsignedShort:</code>), and a int32_t is also a 32 bit int (via <code>numberWithInt:</code>). Although, in the case of <code>-testA</code> the value is also known, so a more appropriate constructor could also be called there -- so the compiler is only width-minimizing based on type, not type and value. when the type of an enum is unspecified or specified as an unsigned type, then you may see promotions like this.</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