xcode 在标签中显示 NSNumber

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7973639/
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-14 22:18:02  来源:igfitidea点击:

Display NSNumber In Label

iphoneobjective-ciosxcode

提问by Ahan Malhotra

I am trying to display an NSNumber from an array with keys into an UILabel. Here is my current code: marblesNeeded.text = [[[records objectAtIndex:0] valueForKey: @"marblesneeded"] intValue];

我正在尝试将数组中的 NSNumber 显示为 UILabel 中的键。这是我当前的代码:marblesNeeded.text = [[[records objectAtIndex:0] valueForKey: @"marblesneeded"] intValue];

I also get the error: warning: Semantic Issue: Incompatible integer to pointer conversion assigning to 'NSString *' from 'int'

我也收到错误: warning: Semantic Issue: Incompatible integer to pointer conversion assigning to 'NSString *' from 'int'

Thanks

谢谢

回答by NJones

You need to create an NSString to set the text of a UILabel.

您需要创建一个 NSString 来设置 UILabel 的文本。

marblesNeeded.text = [NSString stringWithFormat:@"%i",[[[records objectAtIndex:0] valueForKey: @"marblesneeded"] intValue]];

In the format %i denotes that you will be providing an integer value after the format.

在格式 %i 中,您将在格式后提供一个整数值。

Edit:

编辑:

As some comments have noted NSNumber does have a stringValue, it does work but is not my personal preference because it gives you little control as to the format of the string. Consider this example.

正如一些评论所指出的,NSNumber 确实有一个 stringValue,它确实有效,但不是我个人的偏好,因为它使您几乎无法控制字符串的格式。考虑这个例子。

NSNumber *number = [NSNumber numberWithFloat:3.25];
NSLog(@"%@",number.stringValue);  // Will print 3.25
NSLog(@"%@",[NSString stringWithFormat:@"%i",number.intValue]); // Will print 3

Since the question envolved printing an intValue this more explicit format may be necessary.

由于问题涉及打印 intValue 这种更明确的格式可能是必要的。

回答by Owen Hartnett

marblesNeeded.text = [[[records objectAtIndex:0] valueForKey: @"marblesneeded"] stringValue];