Changerrs
Changerrs

Reputation: 273

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.

Upvotes: 8

Views: 35910

Answers (4)

William Falcon
William Falcon

Reputation: 9813

Try using the following,

someLabel.text = "\(someVar)"

Upvotes: 7

Mrigraj
Mrigraj

Reputation: 137

you can do something like this:

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

Upvotes: 4

Tirth
Tirth

Reputation: 7789

Try this

someLabel.text = "\(someVar)"

Yeah

someLabel.text = String(someVar)

Go through this doc https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html

Upvotes: 3

Mick MacCallum
Mick MacCallum

Reputation: 130193

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)

Upvotes: 32

Related Questions