Get an Int out of an UILabel Swift

后端 未结 6 1896
执笔经年
执笔经年 2021-01-20 17:22

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

6条回答
  •  庸人自扰
    2021-01-20 18:08

    For Swift 3, what you can do is to directly convert it from an String input to an integer, like this

    Int(input.text!)
    

    And then, if for any reason, if you wish to print it out or return is as a String again, you can do

    String(Int(input.text!)!)
    

    The exclamation mark shows that it is an optional.

提交回复
热议问题