0

Why is this not working?

NSString *username = [[GKLocalPlayer localPlayer] alias];

random(); { 
    int text = rand() % 4;
    switch (text) {
        case 0: mainTextController.text = username,@"LEOONS"; break;
        case 1: mainTextController.text = username,@"AAIING"; break;
        case 2: mainTextController.text = username,@"AALBES"; break;
        case 3: mainTextController.text = username,@"AALDIJK"; break;


        default:
        break;        }
}

tried with different setups but just won't work

Kevin
  • 291
  • 4
  • 15

3 Answers3

5

The issue is mainTextController.text = username,@"LEOONS";. Comma does not concatenate Objective-C string objects (nor C strings, for the record). What you mean is:

mainTextController.text = [username stringByAppendingString:@"LEOONS"];

(Further info on how the comma works in C: http://en.wikipedia.org/wiki/Comma_operator. I believe in this case, you're setting text to username and then evaluating, but not using the result of, @"LEOONS".)

andyvn22
  • 14,696
  • 1
  • 52
  • 74
  • Yes I know because it was only showing the username so I tried with [] because within those I mostly use comma's but ill check that out thanks for the answer you made my day :D :) ! – Kevin Jun 10 '12 at 15:23
  • 1
    Be sure to check out fabian789's answer as well; as he points out, your entire method could be improved. – andyvn22 Jun 10 '12 at 15:30
3

As andyvn22 said, you need to use -stringByAppendingString: to join two strings. I would like to add that your problem could be solved much nicer like this:

NSString *username = [[GKLocalPlayer localPlayer] alias];
NSArray *randomAddons = [NSArray arrayWithObjects:
                         @"LEOONS",
                         @"AAIING",
                         @"AALBES"
                         /* Add as many as you want here.... */, nil];
int randomIndex = arc4random() % [randomAddons count];
mainTextController.text = [username stringByAppendingString:[randomAddons objectAtIndex:randomIndex]];

(See this for information about arc4random())

Community
  • 1
  • 1
fabian789
  • 8,348
  • 4
  • 45
  • 91
2

Maybe you wanted, to be more random and more future proof :-)

NSString *username = [[GKLocalPlayer localPlayer] alias];
int text = arc4random_uniform(4); //Don't do this: rand() % val or arc4random() % val
switch (text) {
    case 0: mainTextController.text = [NSString stringWithFormat:@"%@ %@ ",username,@"LEOONS"]; break;
    case 1: mainTextController.text = [NSString stringWithFormat:@"%@ %@ ",username,@"AAIING"]; break;
    case 2: mainTextController.text = [NSString stringWithFormat:@"%@ %@ ",username,@"AALBES"]; break;
    case 3: mainTextController.text = [NSString stringWithFormat:@"%@ %@ ",username,@"AALDIJK"]; break;
    default:
        break;
}

but better would be:

NSMutableArray* names = [[NSMutableArray alloc] init];
[names addObject:@"LEOONS"];
[names addObject:@"AAIING"];
[names addObject:@"AALBES"];
[names addObject:@"AALDIJK"];

NSString *username = [[GKLocalPlayer localPlayer] alias];
int text = arc4random_uniform([names count]); //again use arc4random_uniform
mainTextController.text = [NSString stringWithFormat:@"%@ %@ ",username,[names objectAtIndex:text]];
Flori
  • 2,453
  • 1
  • 21
  • 31