Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display Int to screen/label SWIFT

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

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.

like image 884
Changerrs Avatar asked Sep 19 '14 04:09

Changerrs


2 Answers

This is not a valid type cast in Swift.

(String)someVar

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

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

someLabel.text = String(someVar)
like image 198
Mick MacCallum Avatar answered Oct 19 '22 03:10

Mick MacCallum


Try using the following,

someLabel.text = "\(someVar)"
like image 7
William Falcon Avatar answered Oct 19 '22 04:10

William Falcon