views:

154

answers:

3

Hi, I want to get

NSString * test1 = @"one";
 NSString * test2 = @"two";

to my UITextView. I wrote this:

  uitextview.text = (@"%@, %@",test1, test2 );

but the UITextView only gets the second NSString... Why?

+6  A: 

You're passing a format string into a method that doesn't accept format strings. A better way is to append one string to the other using stringByAppendingString:

uitextview.text = [test1 stringByAppendingString:test2];
Jeff Kelley
Thanks, now he writes onetwo, but how can I get a , between them?
Flocked
See DyingCactus' example for that.
Jeff Kelley
Yes i just saw it ;)
Flocked
+10  A: 

You can also do this:

uitextview.text = [NSString stringWithFormat:@"%@, %@", test1, test2];
DyingCactus
+1  A: 

You can't just write @"%@ %@" and expect it to be treated as a format string. It's just an ordinary NSString containing some weird characters. To have it used as a format string, you have to pass it to a method or function that will treat it as such. In this case, you want [NSString stringWithFormat:@"%@ %@",test1, test2].

Chuck