Swift 4.2 解包问题 (??)

Swift 4.2 unwrap issue with (??)

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")