2

I am currently using this code to put some text on the screen

   userOutput.text=@"";

I want to be able to display an int variable in the string, like you do with printf in C with the %d placeholders. How do I do this with an NSString

Regan
  • 1,487
  • 2
  • 28
  • 43

2 Answers2

5
int whatever = 42;
userOutput.text = [NSString stringWithFormat:@"%d", whatever];
walkytalky
  • 9,453
  • 2
  • 36
  • 44
2

use NSString's +stringWithFormat: method:

 userOutput.text= [NSString stringWithFormat: @"%d", 100];

It uses exactly the same format specifiers as printf function in c.

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • 3
    And besides the C `printf` format specifiers, you can use `%@`, to print the `-description` of any object. – Douwe Maan Aug 25 '10 at 22:57