swift How to cast from Int? to String

前端 未结 11 1276
天命终不由人
天命终不由人 2021-02-03 20:42

In Swift, i cant cast Int to String by:

var iString:Int = 100
var strString = String(iString)

But my variable in Int? , there for error:

11条回答
  •  隐瞒了意图╮
    2021-02-03 21:10

    You can try this to convert Int? to string

    let myString : String = "42"
    let x : Int? = myString.toInt()
    
    let newString = "\(x ?? 0)"
    print(newString)        // if x is nil then optional value will be "0"
    

提交回复
热议问题