Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check index of object in NSMutableArray if objects are same?
    primarykey
    data
    text
    <p>I have an NSMutableArray of NSNumbers. I enumerate over array, and for each "1" in there I need to check its position in array and do some stuff. This is how I do it:</p> <pre><code>for (NSNumber *number in fieldArray) { int temp = [number intValue]; if (temp != 0) { if (temp == 1) { switch ([fieldArray indexOfObject:number]) { case 0: { CCSprite *x = [CCSprite spriteWithFile:@"x.png"]; x.position = ccp(53,265); [self addChild:x]; } break; case 1: { //this code never gets called } </code></pre> <p>As I have a "1" at position zero of the array my switch statement never going to go anywhere but number 1. How do I fix that? How to check the position of the stuff in array cell?</p> <p>Why I need this and why I do it this way. I'm making a tic-tac-toe game. I store field in NSMutableArray with capacity of 9. So each number in array is a cell on play field. If the cell is empty, the array holds zero for that position, if there is x - there is 1 value in array, and if there is zero in cell - the array holds 2. When drawing time comes I iterate over array and check what value I see. If the value is zero I do nothing, but if it is one, I draw at certain position. Maybe there is easier way to do this, and you can point me in that direction if there is no way to fix my problem.</p> <p>EDIT: Working solution was to change the enumeration to usual loop.</p> <pre><code> for (int i = 0; i &lt; 9; i++) { int temp = [[fieldArray objectAtIndex:i] intValue]; if (temp == 1) { switch (i) { case 0: { CCSprite *x = [CCSprite spriteWithFile:@"x.png"]; x.position = ccp(53, 265); [self addChild:x z:1]; break; } case 1: { CCSprite *x = [CCSprite spriteWithFile:@"x.png"]; x.position = ccp(159, 265); [self addChild:x z:1]; break; } </code></pre>
    singulars
    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. This table or related slice is empty.
    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