这是我的Playground代码:
let nickName: String? = nil
let fullName: String = "John Appleseed"
let informalGreeting = "Hi \(nickName ?? fullName)"
Hi John
let informalGreeting2 = "Hi \(fullName ?? nickName)"
你好 Optional("John Appleseed")
我认为我理解了第一个案例
let informalGreeting = "Hi \(nickName ?? fullName)"
nickName为空,所以输出应该是"Hi \(fullName)" => "Hi John Appleseed"
在第二种情况下
let informalGreeting2 = "Hi \(fullName ?? nickName)"
如果fullName有值,则输出应该是"Hi \(fullName)" => "Hi John Appleseed" in my opinion。
但是为什么输出是Optional("John Appleseed")这样的可选包装输出呢?
Hi Optional("John Appleseed")