Get integer value from string in swift

后端 未结 12 1845
小蘑菇
小蘑菇 2020-11-27 17:14

So I can do this:

var stringNumb: NSString = \"1357\"

var someNumb: CInt = stringNumb.intValue

But I can\'t find the way to do it w/ a

12条回答
  •  情话喂你
    2020-11-27 17:53

    The method you want is toInt() -- you have to be a little careful, since the toInt() returns an optional Int.

    let stringNumber = "1234"
    let numberFromString = stringNumber.toInt()
    // numberFromString is of type Int? with value 1234
    
    let notANumber = "Uh oh"
    let wontBeANumber = notANumber.toInt()
    // wontBeANumber is of type Int? with value nil
    

提交回复
热议问题