Swift中的枚举模式匹配

Swift中的枚举模式匹配,swift,design-patterns,enums,matching,Swift,Design Patterns,Enums,Matching,我刚开始学习Swift并试图理解模式匹配 我发现了下一个例子: private enum Entities{ case Operand(Double) case UnaryOperation(Double -> Double) case BinaryOperation((Double, Double) -> Double) } 然后我使用模式匹配来确定实体的类型 func evaluate(entity: Entities) -> Double? { sw

我刚开始学习Swift并试图理解模式匹配

我发现了下一个例子:

private enum Entities{
  case Operand(Double)
  case UnaryOperation(Double -> Double)
  case BinaryOperation((Double, Double) -> Double)
}
然后我使用模式匹配来确定实体的类型

func evaluate(entity: Entities) -> Double? {
    switch entity{
    case .Operand(let operand):
        return operand;

    case .UnaryOperation(let operation):
        return operation(prevExtractedOperand1);

    case .BynaryOperation(let operation):
        return operation(prevExtractedOperand1, prevExtractedOperand2);
    }
}
获取关联值的语法似乎有点奇怪,但它工作得很好

之后,我发现可以在
if
语句中使用模式匹配,所以我尝试对
if

if case entity = .Operand(let operand){
    return operand
}
但编译器抛出错误分隔符,我怀疑它和错误的真正原因并没有什么共同之处


您能帮我理解一下,我在
if
语句中尝试使用模式匹配有什么问题吗

我认为您需要的语法是:

if case .Operand(let operand) = entity {
    return operand
}
或者这个:

if case let .Operand(operand) = entity {
    return operand
}

要绑定的变量需要位于
=
登录a
let

的左侧。谢谢你,你救了我一天。现在它完全有道理了。