Articles, podcasts and news about Swift development, by John Sundell.

Expressively comparing a value with a list of candidates

Published on 20 Dec 2017

Continuing to experiment with expressive ways of comparing a value with a list of candidates in Swift. Adding an extension on Equatable is probably my favorite approach so far.

extension Equatable {
    func isAny(of candidates: Self...) -> Bool {
        return candidates.contains(self)
    }
}

let isHorizontal = direction.isAny(of: .left, .right)