iosobjective-cstringcclabelttf

Displaying two different types of variables in one label string


I am trying to make a CCLabelTTF display a string and an integer together. Like this:

Your score is 0.

I've tried a few things but I usually get the warning Data argument not used by format string, and the label doesn't output the correct statements.

I am just trying to figure out the format in which to put these in and searching Google hasn't provided much, as I'm not really sure what exactly to search.

I've tried

label.string = (@"%@", @"hi", @"%d", investmentsPurchased);

but obviously that isn't correct. How would I do this?

Thanks.


Solution

  • (I assume this is ObjC and not Swift.) Try something like this:

    label.string = [NSString stringWithFormat:@"hi %d", investmentsPurchased];
    

    You use a single format string, which contains static text and replacement tokens (like %d) for any replacement variables. Then follows the list of values to substitute in. You can use multiple variables like:

    label.string = [NSString stringWithFormat:@"number %d and a string %@", someInteger, someString];