Swift: How to convert String to UInt?

后端 未结 5 999
太阳男子
太阳男子 2021-02-15 00:42

According to Swift - Converting String to Int, there\'s a String method toInt().

But, there\'s no toUInt() method. So, how to conv

5条回答
  •  遥遥无期
    2021-02-15 01:22

    Use Forced Unwrapping or Optional Binding to make sure the string can be converted to UInt. eg:

    let string = "123"
    let number = UInt(string) //here number is of type *optional UInt*
    
    //Forced Unwrapping
    
    if number != nil {
      //converted to type UInt
    }
    

提交回复
热议问题