swift How to cast from Int? to String

前端 未结 11 1309
天命终不由人
天命终不由人 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:18

    Sonrobby, I believe that "Int?" means an optional int. Basically, by my understanding, needs to be unwrapped.

    So doing the following works fine:

    let y: Int? = 42
    let c = String(y!)
    

    That "!" unwraps the variable. Hope this helps!

    As rakeshbs mentioned, make sure the variable won't be nill.

提交回复
热议问题