swift - 我在 Swift 中遇到 Nil Coalescing 问题

标签 swift null-coalescing-operator

有没有办法在 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/

相关文章:

swift - WatchOS 从 map 导航回应用程序

C#:使用可空 bool 的空合并运算符给出(我认为的)不正确的结果

c# - 表达式树 lambda 不能包含空传播运算符

c# - 如何将 collasce null 运算符与 DbNull.Value 一起使用?

c# - Null Coalesce 与颜色

c# - 如何使用 CodeDOM 表达空合并运算符?

swift - 用外行的话来说,zip 有什么作用?

ios - 监控 iPhone 已解锁多长时间

ios - Swift 3 游戏按钮按下循环

access-modifiers - Swift 有访问修饰符吗?