简体   繁体   中英

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.

(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];

use NSString newString = [NSString stringWithFormat:@"hello %@", investmentsPurchased];

in short: use stringWithFormat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM