Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The order of evaluation of arguments to a function or method is undefined and determined by the compiler. This means that there is no guarantee that the first number will be created before the second, and is inherited by objective-c from c. It appears that on the iPad, they are being executed in the same order that they are in the code, but for the simulator they are created in reverse order. This is the order of events:</p> <p>iPad:</p> <ol> <li>Evaluate first argument. This is a method. Its arguments must be processed: <ol> <li>The argument is the variable <code>a</code>. Use its current value (0).</li> </ol></li> <li>Evaluate second argument. This is a constant string. Load its address.</li> <li>Evaluate third argument. This is a method. Its arguments must be processed: <ol> <li>The argument is an expression: <code>a += 1</code>. Increment <code>a</code> and return its new value (1).</li> </ol></li> <li>Evaluate fourth argument. This is a constant string. Load its address.</li> <li>Evaluate fifth argument. This is a method. Its arguments must be processed: <ol> <li>The argument is an expression: <code>a += 1</code>. Increment <code>a</code> and return its new value (2).</li> </ol></li> <li>Evaluate sixth argument. This is a constant string. Load its address.</li> </ol> <p>Simulator:</p> <ol> <li>Evaluate sixth argument. This is a constant string. Load its address.</li> <li>Evaluate fifth argument. This is a method. Its arguments must be processed: <ol> <li>The argument is an expression: <code>a += 1</code>. Increment <code>a</code> and return its new value (1).</li> </ol></li> <li>Evaluate fourth argument. This is a constant string. Load its address.</li> <li>Evaluate third argument. This is a method. Its arguments must be processed: <ol> <li>The argument is an expression: <code>a += 1</code>. Increment <code>a</code> and return its new value (2).</li> </ol></li> <li>Evaluate second argument. This is a constant string. Load its address.</li> <li>Evaluate first argument. This is a method. Its arguments must be processed: <ol> <li>The argument is the variable <code>a</code>. Use its current value (2).</li> </ol></li> </ol> <p>Since the calling convention of i386, which is used by the simulator, is that arguments are passed on the stack in reverse order, I would guess that this is why the compiler evaluates them in reverse order.</p> <p>To fix your problems, you should create the NSNumber objects before creating the dictionary, forcing the compiler to use the order you want.</p> <pre><code>int a = 0; NSNumber *numA = [[NSNumber alloc] initWithInt:a]; a += 1; NSNumber *numB = [[NSNumber alloc] initWithInt:a]; a += 1; NSNumber *numC = [[NSNumber alloc] initWithInt:a]; NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:numA, @"A", numB ,@"B", numC, @"C", nil]; [numA release]; [numB release]; [numC release]; NSLog(@"%@", d); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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