Swifty Quicky: Checking for a tuple in an array

Screen Shot 2016-05-23 at 4.33.31 PM

let a: [(Int, Int)] = [(1, 2), (3, 4)]
a.contains((3, 4)) // no!
// error: cannot convert value of type '(Int, Int)' 
// to expected argument type '@noescape ((Int, Int)) throws -> Bool'

Screen Shot 2016-05-23 at 4.33.35 PM

let a: [(Int, Int)] = [(1, 2), (3, 4)]
a.contains({ $0 == (3, 4) }) // yes!

Jordan Rose:

Tuples can’t conform to protocols, so they don’t count as Equatable.

2 Comments