ios - Swift 4.2 解包问题 (??)

标签 ios swift4.2

使用 swift 4.2 时遇到奇怪的问题并使用 ?? 解包

简单代码:

var someVar:String?

override func viewDidLoad() {
   someOp = "print some string"

    subTitleLabel.text = "text one" + someVar ?? "unwrapped"
}

编译器抛出错误:

Value of optional type 'String?' must be unwrapped to a value of type 'String'

Swift 4.2 有什么变化?

最佳答案

这是因为 operator precedence .

+的优先级高于??所以要加括号

subTitleLabel.text = "text one" + (someVar ?? "unwrapped")

关于ios - Swift 4.2 解包问题 (??),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53084001/

相关文章:

ios - 恢复码paymentQueue RestoreCompletedTransactions Finished() 是不是必须要有?

iOS 音频单元 : When is usage of AUGraph's necessary?

swift - 修改参数值时使用filter{where :} vs. removeAll{where:}的效率

cocoa - 全屏隐藏 NSToolbar

ios - 标准 UIBarButtonItem 溢出菜单图标

ios - 在 Swift 中比较两种类型时如何理解 "===(identity)"

ios - 使用 Uitextfield 进行用户输入,尝试转换为 int,如果不是 int 则返回 0

ios - 如何使用 Swift 从 tableview 和 sqlite3 中删除

ios13 - UIApplication.shared.beginBackgroundTask 不适用于 iOS 13

ios - 没有这样的模块 'Firebase' ,无法将 Firebase 导入 Swift 4