有没有办法在 Swift 的 nil 合并运算符中不使用强制展开?
我尝试使用:
Int(errorCodeString?) ?? 500
但是,这没有用。
整体代码片段为:
var errorCodeString: String?
errorCodeString = "404"
let actualErrorCode = Int(errorCodeString!) ?? 500
print("Actual Error Code: \(actualErrorCode)")
最佳答案
Int
初始化器不接受可选值。
你的 Int(errorCodeString?) ?? 500
在语法上是不正确的。使用 Int(errorCodeString ?? "") ?? 500
代替。
关于swift - 我在 Swift 中遇到 Nil Coalescing 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38365028/