Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8408315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:35:55+00:00 2026-06-09T23:35:55+00:00

I have an NSMutableArray of NSNumbers. I enumerate over array, and for each 1

  • 0

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:

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
                }

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?

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.

EDIT:
Working solution was to change the enumeration to usual loop.

 for (int i = 0; i < 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;
                }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:35:57+00:00Added an answer on June 9, 2026 at 11:35 pm

    I think I’d avoid the enumeration for this one and look at array positions first, then content. (This isn’t checked in the compiler but should give you the idea. I assume it would be possible to write the positionOfSpriteForIndex: method to return the correct ccp() value.)

    for (int idx = 0; idx < [fieldArray count]; idx++) {
        NSNumber *number = [fieldArray objectAtIndex:idx];
        CCSprite *x = nil;
        switch ([number intValue]) {
            case 1:
                x = [CCSprite spriteWithFile:@"x.png"];
                x.position = [self positionOfSpriteForIndex:idx];
                [self addChild:x];
                break;
    
            case 2:
                x = [CCSprite spriteWithFile:@"o.png"];
                x.position = [self positionOfSpriteForIndex:idx];
                [self addChild:x];
                break;
    
            default:
                break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a NSMutableArray of NSNumbers . Basically I just want to check if
I have this mutable array: myIntegers = [NSMutableArray array]; [myIntegers addObject:[NSNumber numberWithInteger:indexDelete - 1]];
I have an NSMutableArray populated with NSIntegers. I need to loop through the array.
I have a NSMutableArray pages with several objects (instances of Page ). Each Page
I have a NSMutableArray: NSMutableArray *temp = //get list from somewhere. Now there is
I only have this code in m. file NSMutableArray * arrayOfBools; arrayOfBools=[[NSMutableArray alloc] initWithCapacity:1000];
I have an NSMutable array of N elements. Each element is also an NSMutable
I have 2D array Like following: NSMutableArray *world=[[NSMutableArray alloc]init]; NSNumber *number; NSMutableArray *inner; int
I have an NSMutableArray with many objects in it. Some are NSStrings, others are
There are lots of examples on this site about adding to NSMutableArray and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.