我想匹配多个枚举并且有这样的东西:
guard case .north = $0, case .south = $0 else { return }
有没有办法将其压缩为这样的单个语句?
guard case (. north, . south) = $0 else { return }
上面没有编译,但希望我能做这样的事情。有替代方案吗?
最佳答案
您可以将所需的案例放入文字数组中,并使用 contains
来测试匹配:
guard [.north, .south].contains($0) else { return }
关于swift - 将多个枚举与单个 guard 案例匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40191791/