How to do if pattern matching with multiple cases?

后端 未结 3 1514
一整个雨季
一整个雨季 2020-12-06 10:20

I\'m searching for the syntax to do pattern matching with multiple cases in an if case statement. The example would be this:

enum Gender {
    case Male, Fem         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 10:48

    You should use a collection. In JavaScript I would write something like this:

    if ([Gender.Male, Gender.Female].includes(actualGender))
        console.log(actualGender);
    

    Note that I have not a clue about swift, or how to do the same in that language, so here is a relevant answer in the topic: https://stackoverflow.com/a/25391725/607033 :D

    EDIT: This is the Swift version:

    if [.Male, .Female].contains(a) {
    
    }
    

提交回复
热议问题