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

bguy

macrumors newbie
Original poster
May 30, 2009
28
0
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.)
 
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
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.