NSString Troubles

Discussion in 'iOS Programming' started by Programmer, Oct 4, 2009.

  1. Programmer macrumors member

    Programmer

    Joined:
    Jun 16, 2009
    #1
    In my application i have a score and a high score string. I set up a string the UIAlertViewBody NSString set up which sets the text in my UIAlertView message, it tells it to say "Your Score Was (scoreString) Your High Score Is (HighScoreS)". But when i run the application on my phone it just says "Your Score was" when the UIAlertView Pops up.

    Here's the rest of the code.

    Code:
    NSString *scoreString = [[NSString alloc] initWithFormat:@"%d", score];
    NSString *HighScoreS = [[NSString alloc] initWithFormat:@"%d", Highscoreint];
    NSString *UIAlertViewBody = [NSString stringWithFormat:@"Your Score Was", [NSString stringWithFormat:scoreString], [NSString stringWithFormat:@"Your High Score Is"], [NSString stringWithFormat:HighScoreS]];
    
    UIAlertView *YouWinHighScore = [[UIAlertView alloc] initWithTitle:@"Score" message:UIAlertViewBody delegate:self cancelButtonTitle:@"Close" otherButtonTitles:@"Challenge A Friend By Email", @"Challenge A Friend By Text", nil];
    	[YouWinHighScore show];
    	[YouWinHighScore release];
     
  2. plinx0r macrumors member

    Joined:
    Jul 11, 2008
  3. Programmer thread starter macrumors member

    Programmer

    Joined:
    Jun 16, 2009
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Code:
    NSString *UIAlertViewBody = [NSString stringWithFormat:@"Your Score Was %@ Your High Score Is %@", scoreString, HighScoreS];
     
  5. Programmer thread starter macrumors member

    Programmer

    Joined:
    Jun 16, 2009
  6. chbeer macrumors member

    chbeer

    Joined:
    Sep 22, 2008
    Location:
    Berlin
    #6
    Why don't you simply do:

    Code:
    NSString *UIAlertViewBody = [NSString stringWithFormat:@"Your Score Was %d Your High Score Is %d", score, Highscoreint];
    
    UIAlertView *YouWinHighScore = [[UIAlertView alloc] initWithTitle:@"Score" message:UIAlertViewBody delegate:self cancelButtonTitle:@"Close" otherButtonTitles:@"Challenge A Friend By Email", @"Challenge A Friend By Text", nil];
    [YouWinHighScore show];
    [YouWinHighScore release];
    And for an internationalization you should use NSLocalizedString. That way you can later place the text in a .strings file and let it translate to other languages:
    Code:
    NSString *UIAlertViewBody = [NSString stringWithFormat:NSLocalizedString(@"Your Score Was %d Your High Score Is %d", @""), score, Highscoreint];
    And in Localized.strings for english:
    Code:
    "Your Score Was %d Your High Score Is %d" = "Your Score Was %d Your High Score Is %d";
    And in Localized.strings for german:
    Code:
    "Your Score Was %d Your High Score Is %d" = "Ihre Punktzahl war %d Ihre Punktzalh ist %d";
     

Share This Page