Image array with Obj-C

Discussion in 'Mac Programming' started by Hot Sauce79, Jul 21, 2008.

  1. Hot Sauce79 macrumors newbie

    Joined:
    Jul 1, 2008
    #1
    How would i go about creating and working with an image array in Obj-C in Xcode?

    I need to know how to create the array to be 10 by 20 and i need to be able to swap in and out images in the array. (Images are 20 x 20 pixels if that matters)

    I will swap the images using if statements of course, but I do not know/can not find the syntax for working with arrays in Obj-C.

    Any and all help is appreciated.
     
  2. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
  3. Hot Sauce79 thread starter macrumors newbie

    Joined:
    Jul 1, 2008
    #3
    Ok i understand how to declare them now... all except for one part that is.

    here is kinda of what i want to do in Xcode with C++ syntax
    Code:
    int Array[10][20];
    
    ...
    array[0][19] = blueimage;
    array[0][18] = redimage;
    array[0][17] = blueimage;
    array[0][16] = greenimage;
    ...
    array[0][1] = blueimage;
    array[0][0] = greenimage;
    ...
    
    
    for (i=0; i<=9; i++){
    array[0][x]=blackimage;
    x=x+1;
    y=25;
    }
    
    if (y=25)
    {
    code that changes all of array[0][...] to array[1][...]
    }
    
    That isnt exactly what I am needing to do, but if i knew how to do all of that in Xcode then I would be able to figure the rest out on my own.

    I think I just need to learn how to make multiple indexes for a single object in the array but i can't find any syntax for it. =/ i looked at that page that you provided but I couldn't find it on there.
     
  4. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #4
    C-style arrays in Obj-C are exactly the same as arrays in C. However, I'd closely follow the advice not to use built in C-arrays for objects of class (or NSObject derived) type. There are many pitfalls in doing so (lookup 'never treat arrays polymorphically' in google).

    As a previous poster suggested, use NSMutableArray instead.
     
  5. Hot Sauce79 thread starter macrumors newbie

    Joined:
    Jul 1, 2008
    #5
    ok. I understand.


    I also think i might have figured out another way to do it though...

    would making an array with 200 indexes and calling a function that translates what i want to happen into the 1-200 number that corresponds with my [0-9][0-19] number work?

    im guessing that i could do this by running a computation on it. It seems that it will work well enough.
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    I think what you want is just to have a multidimensional array. The good news is that NSMutableArray holds NSObjects, and NSMutableArray is an NSObject. The bad news is that this does not guarantee a rectangular matrix. Each of the second-level NSMutableArrays might have a different number of elements, making the array "jagged". You can make it rectangular, it's just not something that can be assumed. You can use -initWithCapacity or +arrayWithCapacity to ensure that the second-level NSMutableArrays are all can contain y elements at instantiation, but note that you must place objects in order from index 0 to index y-1. You would do something like:

    Code:
    NSMutableArray *myArray = [[NSMutableArray alloc] initWithCapacity:20];
    for(NSUInteger x=0;x<20;x++) {
      [myArray insertObject:[[NSMutableArray alloc] initWithCapacity:15] atIndex: x];
    }
    for(NSUInteger x=0;x<20;x++) { 
      for(NSUInteger y=0;y<15;y++) {
        [[myArray objectAtIndex:x] insertObject: [NSNumber numberWithUnsignedInteger:x*15+y] atIndex:y];
      }
    }
    
    NSNumber *oneVal = [[myArray objectAtIndex:4] objectAtIndex:5];
    NSLog(@"The value is: %@.",oneVal);
    
    The output should be:
    The value is 65.

    I didn't compile this, but I hope the idea is clear and some of the methods you should use are included. In C this would be similar to:

    Code:
    int myArray[20][15],x,y;
    for(x=0;x<20;x++) {
      for(y=0;y<15;y++) {
        myArray[x][y]=x*15+y;
      }
    }
    Instead you are storing objects instead of primitives, and your arrays can be dynamically resized after the initial setup if needed.

    -Lee
     
  7. Hot Sauce79 thread starter macrumors newbie

    Joined:
    Jul 1, 2008
    #7
    Thank you very much everyone (especially Lee) you all helped me out alot.
     
  8. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #8
    Hi Yeroen, I understand the problem you mention wrt C++, but I would have thought in this case C style arrays would be fine since the array would really just be an array of typed pointers.

    thanks

    b e n
     

Share This Page