Swift多值比较(扩展Equatable)

2019-05-23  本文已影响0人  JonorZhang

Talk is cheap, show you my code below:

extension Equatable where Self: Any {
    /// 是否等于给定序列的所有值
    ///
    ///     普通写法: if a == b && a == c && a == d { //true }
    ///     高级写法: if a.isEqual(allOf: b, c, d) { //true }
    /// - Parameter them: 要比较的参数序列
    /// - Returns: 是否等于
    public func isEqual(allOf them: Self...) -> Bool {
        for e in them {
            if e != self { return false }
        }
        return true
    }
    
    /// 是否等于给定序列的任意值
    ///
    ///     普通写法: if a == b || a == c || a == d { //true }
    ///     高级写法: if a.isEqual(oneOf: b, c, d) { //true }
    /// - Parameter them: 要比较的参数序列
    /// - Returns: 是否等于
    public func isEqual(oneOf them: Self...) -> Bool {
        for e in them {
            if e == self { return true }
        }
        return false
    }
}
上一篇 下一篇

猜你喜欢

热点阅读