views:

60

answers:

1

hello everyone, I'm a new "developer" trying to build some iPhone app I'm making an app which gets text from a list of objects in a NSArray and then randomizes them and display one in a TextView, here's the code:

- (IBAction)azione{
NSArray *myArray= [NSArray arrayWithObjects: @"Pasta",@"Pizza",@"Wait",@"Go", nil]; 
int length = [myArray count];
int chosen = arc4random() % length;
testo.text = [myArray objectAtIndex: chosen];

}

what I want to do now, is when I open the app and get a random object, to take it out from the list, so that it won't be picked again

ex. I open the app>I get "Pizza">Do the action again>I don't get "Pizza" anymore, only "Pasta" "Wait" and "Go"

What should I do ? Which code should I use ?

Thanks for answers

A: 

Add the following line at the end of your function.

[myArray removeObjectAtIndex: chosen]
David
That's an NSArray not an NSMutableArray.
KennyTM
I changed it as an NSMutableArray and I coded like this:- (IBAction)azione{ NSMutableArray *myArray= [NSMutableArray arrayWithObjects: @"Pasta",@"Pizza",@"Wait",@"Go", nil]; int length = [myArray count]; int chosen = arc4random() % length; testo.text = [myArray objectAtIndex: chosen]; [myArray removeObjectAtIndex: chosen];}but nothing is happening, as I open the app, get an object and that continues to appearwhat should I do ?Thanks
David Pollak
@Kenny: Thanks, I missed that. @David, is the action getting called more than once?
David
no only once, in that action
David Pollak
It doesn't remember that you've removed an item because you're recreating the array on each call to that function. (D'oh!) You may have to create the array in -init and store it in an instance variable.
codewarrior
You're right, I'm trying hard to do something but it doesn't happen anything, could you give me a code sample please ?Thanks
David Pollak