Preferred approach to check optionals in Swift
问题 In working with optionals in Swift, there seems to be two approaches to check if an optional type is nil or not. var item: String? = "apple" // Approach A if item != nil { "item is \(item!)" } else { "no item" } // Approach B if let x = item { "item is " + x } else { "no item" } Does it matter which approach I use to check the optional? 回答1: They are equivalent, but it's better to use: let x = item (optional binding) when you actually need and use the unwrapped value item != nil when you just