Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

philwa2

macrumors member
Original poster
Jul 12, 2008
30
1
Hello,

Im trying to cast an int into a string so that I can output the value to the text property of a label - the code below generates a random number and assigns the value to the variable "generated" I then want to take this value convert it to a string and assign it the text of the label.

Does anyone know how to do this?

- (IBAction)clearText {
int generated;
generated = (random() % 100) + 1;
mainText.text = generated;
}
 

caldwelljason

macrumors member
Jul 9, 2008
44
2
NSString

Use

label.text = [NSString stringWithFormat:mad:"%d", generated];

stringWithFormat works like printf in c. You can pass various formats to control the output and you can have multiple replacement values in the format string. For example,

[NSString stringWithFormat:mad:"You have clicked %d out of %d buttons or %1.2f%%", var1, var2, (double)var1/(double)var2];

might yield, "You have clicked 4 out of 5 buttons or 80.00%"
 

Jeremy1026

macrumors 68020
Nov 3, 2007
2,215
1,029
to put it in the label use the following code
Code:
labelName.text = [[NSNumber numberWithInt:variable] stringValue];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.