Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

howirocks

macrumors newbie
Original poster
May 7, 2008
8
0
Bare with me here...

I'm working on a name generator. I want to use (i think) an NSArray of words to serve as a word bank to randomly pick from. It would be 3 separate words to make a funny random name like "Slippery California Farmers" or something.

So I created and positioned a UILabel for each of the 3 words and used:

- (IBAction)buttonClick:(id)sender {
NSString *title = nil;
NSString *path = nil;


int Number = rand() % 3;
switch (Number) {

case 0:
myTextLabel1.text = @"Slippery";
myTextLabel2.text = @"California";
myTextLabel3.text = @"Farmers";
break;

case 1:
myTextLabel1.text = @"Raisin";
myTextLabel2.text = @"Hell";
myTextLabel3.text = @"Dudes";
break;

case 2:
myTextLabel1.text = @"Abnormal";
myTextLabel2.text = @"Scissor";
myTextLabel3.text = @"Killers";
break;

}

It shows up fine. But how would I randomize to change the text for each label to pick a different word, creating a new name each time?

And could I create my own invisible word bank to pick from?

Hope you understand what I'm trying to say. I'm new to developing.

Thanks in advance.
 
You can populate an NSArray a variety of ways. Here's one:
Code:
[COLOR=#400080]NSArray[/COLOR] [COLOR=#002200]*[/COLOR] firstList [COLOR=#002200]=[/COLOR] [COLOR=#002200][[/COLOR][COLOR=#002200][[/COLOR][COLOR=#400080]NSArray[/COLOR] alloc[COLOR=#002200]][/COLOR] initWithObjects[COLOR=#002200]:[/COLOR][COLOR=#bf1d1a]@[/COLOR][COLOR=#bf1d1a]"Slippery"[/COLOR],[COLOR=#bf1d1a]@[/COLOR][COLOR=#bf1d1a]"Raisin"[/COLOR],[COLOR=#bf1d1a]@[/COLOR][COLOR=#bf1d1a]"Abnormal"[/COLOR],[COLOR=#a61390]nil[/COLOR][COLOR=#002200]][/COLOR];

You can have similar collection for the other two parts of the name.

Then fetch each part of the String based on index (position) in the array using the objectAtIndex: method -

Code:
[firstList objectAtIndex:Number]
 
Code:
NSArray *adjList = [[NSArray alloc] initWithObjects:@"Slippery",@"Red",@"Rasta",@"Natty",nil];
    [adjList objectAtIndex:0;]      <== ON THIS LINE  - Expected ']' before ';' token
    [*adjList release];         <==AND THIS LINE - Expected ']' before ';' 'release'

I used @synthesize above it:

Code:
@synthesize myButton, myTextLabel1, myTextLabel2, myTextLabel3;
@synthesize adjList;
@synthesize verbList;
@synthesize nounList;

What am I doing wrong?
 
Awesome, the errors went away. I appreciate your help. I am definitely learning as I go. I keep forgetting things I've done and read.

If you don't mind, I just need another pointer. Should I link the specific text label outlets to the respected NSArrays I want them to draw from?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.