compare with Optional

Optional ๅฏไปฅ็›ดๆŽฅ่ˆ‡ non-Optional ๆฏ”่ผƒ๏ผŒไฝ†ๅช่ƒฝ็”จ "=="๏ผŒไธ่ƒฝ็”จๅ…ถไป–็š„ ">", "<", ">=", "<=" ็ญ‰้‹็ฎ— (Swift 3 ็งป้™คๆญคๅŠŸ่ƒฝ ๐Ÿ‘‰ SE-0121)

let a = [1, 2, 3, 4, 5]
let b = [1, 3, 5, 7, 9]
let c = [Int]()

// โญ๏ธ comparing two optionals
a.first == b.first        // true
a.last == b.last          // false
a.first == c.first        // false

// โญ๏ธ comparing optional with non-optional
let n = Int("23")         // Optional(23)

// โญ๏ธ literal `23` is inferred as `Int` then wrapped as `Optional`
//    before the comparison is performed
n == 23                    // true

Last updated