String Help

Discussion in 'Mac Programming' started by bguy, Aug 4, 2009.

  1. bguy macrumors newbie

    Joined:
    May 30, 2009
    #1
    Is there a way to have a textfield work like printf in objective c cocoa?
    I want to have one message where one word changes depending on the variable, such as having it say "Hello Jack" and "Hello Sam" depending on input. (I don't want to learn a program to check user input and reply, I just want to know how to have the message be flexible, so I don't have to have a huge if, else if pile to have different messages.)
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    http://developer.apple.com/document.../apple_ref/occ/clm/NSString/stringWithFormat:

    Using stringWithFormat: you can do something like:
    Code:
    NSString *myString = [NSString stringWithFormat:@"Hello, %@!",x==0?@"Sam":@"Jack"];
    Where x is the value you're testing. The resulting NSString pointed to by myString can then be used to set your text field. If you have a lot of options, though, I don't see how you're going to avoid a big if-else or switch block... unless you keep the names in an NSArray of NSStrings and somehow the input you want to check is an index into it, then you could use the stringWithFormat: method and have the argument be the return of an objectAtIndex:.

    -Lee
     
  3. bguy thread starter macrumors newbie

    Joined:
    May 30, 2009
    #3
    Thank you. This will be great, as I already have a switch and if else system put in, I just didn't want to have to do it again.
     

Share This Page