Figure - Custom Switch Matchers
We’ve made a lot of hay out of Swift’s incredibly flexible switch statement on this blog. As we’ve seen, its pattern matching is particularly powerful when dealing with tuples:
switch (xCoordinate, yCoordinate){ case (0, 0): println("origin") case let (0, y): println("No X, \(y) high.") case let (x, 0): println("\(x) far, no Y.") case (0...5, 5...10): println("In first quadrant") case let (x, _): println("Who cares about Y? X is \(x)") } But we don’t have to limit ourselves to tuples or simple value types when using a switch.
figure.ink