tags:

views:

33

answers:

2

I'm trying to add random numbers onto the end of this array when ever I call this function. I've narrowed it down to just this so far. when ever I check to see if anything is in the array it says 0. So now i'm quite stumped. I'm thinking that MutableArray releases the contents of it before I ever get to use them. problem is I don't know what to use. A button calls the function and pretty much everything else is taken care of its just this portion inside the function.

NSMutableArray * choices;

-(void) doSomething {

int r = arc4random() % 4;
//The problem...
[choices addObject:[NSNumber numberWithInt:r]];

int anInt = [[choices objectAtIndex:1] integerValue];
NSLog(@"%d", anInt);
    //I'm getting nothing no matter what I do.

//some type of loop that goes through the array and displays each element 
//after it gets added

}
A: 

You need to initialize choices:

NSMutableArray* choices;
choices = [[NSMutableArray alloc] init];
drewh
I tried to initialize it like this (in the same file it was declared) and I got several errors.
Ohmnastrum
okay I tried it inside of an ibAction and it worked. but what if I wanted the array to persist for the whole game? wouldn't it go out of scope?
Ohmnastrum
Make it a property of your class, e.g. view controller or application delegate. Then you can use it outside of a single method.
Alex Reynolds
+2  A: 

Was choices ever initialized?

...somewhere: choices = [[NSMutableArray alloc] init];

... then, also, array indexes start at 0, so [[colorChoices objectAtIndex:1] won't work the first time the doSomething function is called.

Colin
never did initialize it. Thanks for the reminder ^^; as for starting on 1, I figured that it didnt matter since i pressed it more than 2 times.
Ohmnastrum