swift How to cast from Int? to String

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

    I like to create small extensions for this:

    extension Int {
        var stringValue:String {
            return "\(self)"
        }
    }
    

    This makes it possible to call optional ints, without having to unwrap and think about nil values:

    var string = optionalInt?.stringValue
    

提交回复
热议问题