objective-c 我应该如何将 int 传递给 stringWithFormat?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/384089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:00:30  来源:igfitidea点击:

How should I pass an int into stringWithFormat?

objective-ccocoa-touch

提问by Brennan

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.

我尝试使用 stringWithFormat 在标签的 text 属性上设置数值,但以下代码不起作用。我无法将 int 转换为 NSString。我期待该方法知道如何自动将 int 转换为 NSString。

What do I need to do here?

我需要在这里做什么?

- (IBAction) increment: (id) sender
{
    int count = 1;
    label.text = [NSString stringWithFormat:@"%@", count];
}

回答by BobbyShaftoe

Do this:

做这个:

label.text = [NSString stringWithFormat:@"%d", count];

回答by Marc Charbonneau

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.

请记住,@"%d" 仅适用于 32 位。如果您曾经为 64 位平台编译,一旦开始使用 NSInteger 来实现兼容性,您应该使用 @"%ld" 作为您的格式说明符。

回答by squelart

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.

请记住,@"%d" 仅适用于 32 位。如果您曾经为 64 位平台编译,一旦开始使用 NSInteger 来实现兼容性,您应该使用 @"%ld" 作为您的格式说明符。

Interesting, thanks for the tip, I was using @"%d" with my NSIntegers!

有趣,感谢您的提示,我在我的NSIntegers 中使用了 @"%d" !

The SDK documentation also recommends to cast NSIntegerto longin this case (to match the @"%ld"), e.g.:

SDK 文档还建议在这种情况下强制NSInteger转换long为(以匹配 @"%ld"),例如:

NSInteger i = 42;
label.text = [NSString stringWithFormat:@"%ld", (long)i];

Source: String Programming Guide for Cocoa - String Format Specifiers(Requires iPhone developer registration)

来源:Cocoa 的字符串编程指南 - 字符串格式说明符(需要 iPhone 开发者注册)

回答by Zach Langley

You want to use %dor %ifor integers. %@is used for objects.

您想使用%d%i用于整数。%@用于对象。

It's worth noting, though, that the following code will accomplish the same task and is much clearer.

不过,值得注意的是,以下代码将完成相同的任务,并且更加清晰。

label.intValue = count;

回答by squelart

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)

(虽然如果有一天你正在处理 NSNumber 的话它会很有用)

回答by ohho

To be 32-bit and 64-bit safe, use one of the Boxed Expressions:

为了 32 位和 64 位安全,请使用盒装表达式之一

  label.text = [NSString stringWithFormat:@"%@", @(count).stringValue];

回答by Aruna

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
*/

回答by Abizern

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.

我问的原因是您命名了一个 method increment,但您似乎使用它来设置文本标签的值,而不是增加一个值。

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 countand your incrementaction 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.

您声明一个属性count,您的increment操作将此值设置为任何值,然后在 IB 中,您将标签的文本绑定到 的值count。只要您遵循键值编码 (KVC) 和count,您就不必编写任何代码来更新标签的显示。从设计的角度来看,您的耦合更松散。

回答by Craig Bruce

Don't forget for long long int:

不要忘记long long int

long long int id = [obj.id longLongValue];
[NSString stringWithFormat:@"this is my id: %lld", id]

回答by Rodrigo Carrapeiro

label.text = [NSString stringWithFormat:@"%d", XYZ]; 

//result:   label.text = XYZ
//use %d for int values