André Kuhlmann André Kuhlmann - 1 year ago 37
Swift Question

Get an Int out of an UILabel Swift

I have the problem, to have a high amount of buttons which have a number as their label, so i thought i could take the label as an integer instead of creating an action for every button?!

@IBAction func NumberInput(sender: UIButton) {
var input:Int = sender.titleLabel as Int


If you want to do this, you can convert the string to an Int by using string.toInt() such as:

if let input = sender.titleLabel?.text?.toInt() {
    // do something with input
} else {
    // The label couldn't be parsed into an int

However, I'd suggest either using UIView.tag or subclassing UIButton and adding an Int property to it to accomplish this, in case you ever change the display of your labels.