T1992
T1992

Reputation: 73

Random word generator - IOS development

I'm developing a app where people must click on a button and then the label show a random word.

I can use a switch statement, but the app must have more than 50 words, so to write the switch statement is a lot of work and not very useful.

Do someone have a solution?

Upvotes: 0

Views: 735

Answers (2)

Mick MacCallum
Mick MacCallum

Reputation: 130193

Simple, use an array, and a random number generator.

NSArray *myArray = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
[myLabel setText:[myArray objectAtIndex:arc4random_uniform([myArray count])]];

This should be enough to nudge you in the right direction.

Upvotes: 1

lakshmen
lakshmen

Reputation: 29064

Don't use a switch. Use An array with the fifty items intitalised and use a random number generator to get an random index in the array, which will return a random word.......

Upvotes: 1

Related Questions