Albert K
Albert K

Reputation: 25

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.

Upvotes: 0

Views: 1108

Answers (2)

penduDev
penduDev

Reputation: 4785

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

in short: use stringWithFormat

Upvotes: 1

Ben Zotto
Ben Zotto

Reputation: 71008

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

Upvotes: 2

Related Questions