ios 如何将 Int 显示到屏幕/标签 SWIFT

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

How to display Int to screen/label SWIFT

iosobjective-cswift

提问by Changerrs

I am not sure how to display an int to the screen?

我不确定如何在屏幕上显示 int ?

this is what i've tried:

这是我试过的:

@IBOutlet weak var Button: UIButton!
@IBOutlet weak var someLabel: UILabel!
var someVar = 0

@IBAction func buttonPressed(sender: AnyObject) {
    someVar = someVar + 1  
    someLabel.text = (String)someVar
}

I have linked up the button and label to the view controller.

我已将按钮和标签链接到视图控制器。

Thanks, sorry for the noob question.

谢谢,抱歉菜鸟问题。

回答by Mick MacCallum

This is not a valid type cast in Swift.

这在 Swift 中不是有效的类型转换。

(String)someVar

If String was a valid subtype of Int, you could downcast with the "as" operator, but it isn't.

如果 String 是 Int 的有效子类型,则可以使用“as”运算符进行向下转换,但事实并非如此。

In this case, you want to initialize a new String passing the Int as an argument.

在这种情况下,您想要初始化一个新的 String,将 Int 作为参数传递。

someLabel.text = String(someVar)

回答by William Falcon

Try using the following,

尝试使用以下方法,

someLabel.text = "\(someVar)"

回答by Mrigraj

you can do something like this:

你可以做这样的事情:

someLabel.text = NSString(format:"%d", someVar)