Swift开关模式与数组匹配

时间:2016-08-02 16:01:35

标签: ios arrays swift switch-statement

好奇是否有办法在Swift中执行以下操作。

let foo = [1, 2, 3]
let bar = [4, 5, 6]

let value = 5

switch value {
case in foo
    print("5 is in foo")
case in bar
    print("5 is in bar")
default:
    break
}

我理解还有其他方法可以使这个人为的示例工作,例如case 4, 5, 6:或不使用开关,而是使用bar.contains(value),但我正在寻找一个专门涉及开关模式匹配的解决方案数组。谢谢!

3 个答案:

答案 0 :(得分:24)

怎么样:

let foo = [1, 2, 3]
let bar = [4, 5, 6]

let value = 5

switch value {
case _ where foo.contains(value):
    print("\(value) is in foo")
case _ where bar.contains(value):
    print("\(value) is in bar")
default:
    print("\(value) is not in foo or bar")
}

答案 1 :(得分:18)

您可以定义自定义模式匹配运算符 ~=将数组作为"模式"和值:

func ~=<T : Equatable>(array: [T], value: T) -> Bool {
    return array.contains(value)
}

let foo = [1, 2, 3]
let bar = [4, 5, 6]

let value = 5

switch value {
case foo:
    print("\(value) is in foo")
case bar:
    print("\(value) is in bar")
default:
    break
}

类似的运算符已经存在,例如间隔时间:

public func ~=<I : IntervalType>(pattern: I, value: I.Bound) -> Bool

答案 2 :(得分:0)

@Martin R为什么要停在数组上。符合the manual协议的所有类型都有一个contains实例方法。以下内容与您的答案具有相同的行为,不同之处在于它适用于符合Sequence的任何类型。

func ~=<S : Sequence>(sequence: S, value: S.Element) -> Bool
        where S.Element: Equatable {

    return sequence.contains(value)
}


let items = [1, 2, 3, 4, 5]
let iterable = items.makeIterator()

let value = 3

switch value {
    case iterable:
        print("\(value) is contained in iterable")
    default:
        print("\(value) is NOT contained in iterable")
}

但是,我们可以做得更好

我认为case iterable太神奇了。它并没有传达出我们正在测试valueiterable中的成员资格这一事实。如果我们可以写case containedIn(iterable)会更好。实际上,这实际上是可能的:

public func ~=<T>(pattern: (T) -> Bool, value: T) -> Bool {
    return pattern(value)
}

func containedIn<S : Sequence>(_ sequence: S) -> ((S.Element) -> Bool)
        where S.Element: Equatable {

    return { element in sequence.contains(element) }
}

let arrayOfNumbers = [1, 2, 3, 4, 5]
let setOfNumbers: Set = [5, 6, 7, 8, 9, 10]

let value = 8

switch value {
    case containedIn(arrayOfNumbers):
        print("\(value) is contained in arrayOfNumbers")
    case containedIn(setOfNumbers):
        print("\(value) is contained in setOfNumbers")
    default:
        break
}

唯一的问题是containedIn在switch语句的上下文之外使用时表现异常。