0

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

}
1
  • I've looked around and I don't see how its not working honestly. Commented May 21, 2010 at 20:49

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

You need to initialize choices:

NSMutableArray* choices;
choices = [[NSMutableArray alloc] init];

3 Comments

I tried to initialize it like this (in the same file it was declared) and I got several errors.
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?
Make it a property of your class, e.g. view controller or application delegate. Then you can use it outside of a single method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.