Reputation: 11686
I am try to use stringWithFormat to set a numerical value on the text property of a label but the following code is not working. I cannot cast the int to NSString. I was expecting that the method would know how to automatically convert an int to NSString.
What do I need to do here?
- (IBAction) increment: (id) sender
{
int count = 1;
label.text = [NSString stringWithFormat:@"%@", count];
}
Upvotes: 69
Views: 136955
Reputation: 147
NSString * formattedname;
NSString * firstname;
NSString * middlename;
NSString * lastname;
firstname = @"My First Name";
middlename = @"My Middle Name";
lastname = @"My Last Name";
formattedname = [NSString stringWithFormat:@"My Full Name: %@ %@ %@", firstname, middlename, lastname];
NSLog(@"\n\nHere is the Formatted Name:\n%@\n\n", formattedname);
/*
Result:
Here is the Formatted Name:
My Full Name: My First Name My Middle Name My Last Name
*/
Upvotes: 1
Reputation: 51921
To be 32-bit and 64-bit safe, use one of the Boxed Expressions:
label.text = [NSString stringWithFormat:@"%@", @(count).stringValue];
Upvotes: 6
Reputation: 1
label.text = [NSString stringWithFormat:@"%d", XYZ];
//result: label.text = XYZ
//use %d for int values
Upvotes: 0
Reputation: 674
Don't forget for long long int
:
long long int id = [obj.id longLongValue];
[NSString stringWithFormat:@"this is my id: %lld", id]
Upvotes: 1
Reputation: 150605
Is the snippet you posted just a sample to show what you are trying to do?
The reason I ask is that you've named a method increment
, but you seem to be using that to set the value of a text label, rather than incrementing a value.
If you are trying to do something more complicated - such as setting an integer value and having the label display this value, you could consider using bindings. e.g
You declare a property count
and your increment
action sets this value to whatever, and then in IB, you bind the label's text to the value of count
. As long as you follow Key Value Coding (KVC) with count
, you don't have to write any code to update the label's display. And from a design perspective you've got looser coupling.
Upvotes: 1
Reputation:
Marc Charbonneau wrote:
Keep in mind that @"%d" will only work on 32 bit. Once you start using NSInteger for compatibility if you ever compile for a 64 bit platform, you should use @"%ld" as your format specifier.
Interesting, thanks for the tip, I was using @"%d" with my NSInteger
s!
The SDK documentation also recommends to cast NSInteger
to long
in this case (to match the @"%ld"), e.g.:
NSInteger i = 42;
label.text = [NSString stringWithFormat:@"%ld", (long)i];
Source: String Programming Guide for Cocoa - String Format Specifiers (Requires iPhone developer registration)
Upvotes: 41
Reputation:
And for comedic value:
label.text = [NSString stringWithFormat:@"%@", [NSNumber numberWithInt:count]];
(Though it could be useful if one day you're dealing with NSNumber's)
Upvotes: 13
Reputation: 40517
Keep in mind that @"%d" will only work on 32 bit. Once you start using NSInteger for compatibility if you ever compile for a 64 bit platform, you should use @"%ld" as your format specifier.
Upvotes: 49
Reputation: 6786
You want to use %d
or %i
for integers. %@
is used for objects.
It's worth noting, though, that the following code will accomplish the same task and is much clearer.
label.intValue = count;
Upvotes: 23
Reputation: 28499
Do this:
label.text = [NSString stringWithFormat:@"%d", count];
Upvotes: 133