Swift – Enum返回错误的值类型

我遇到以下代码问题:

private enum Op : Printable {
    case Operand(Double)
    case Constant(String, Double)
    case Variable(String)
    case UnaryOperation(String, Double -> Double)
    case BinaryOperation(String, (Double, Double) -> Double)
    var description: String {
        get {
            switch self {
            case .Operand(let operand):
                return "\(operand)"
            case .Constant(let constant, _):
                return constant
            case .Variable(let variable):
                return variable
            case .UnaryOperation(let symbol, _):
                return symbol
            case .BinaryOperation(let symbol, _):
                return symbol
            }
        }
    }
}

private var knownOps = [String:Op]()

init() {
    func learnOp(op: Op) {
        knownOps[op.description] = op
    }
    learnOp(Op.Constant("π", M_PI))
    learnOp(Op.UnaryOperation("√", sqrt))
    learnOp(Op.UnaryOperation("sin", sin))
    learnOp(Op.UnaryOperation("cos", cos))
    learnOp(Op.UnaryOperation("±") { -1 * $0 })
    learnOp(Op.BinaryOperation("×", *))
    learnOp(Op.BinaryOperation("÷") { $1 / $0 })
    learnOp(Op.BinaryOperation("+", +))
    learnOp(Op.BinaryOperation("-") { $1 - $0 })
}

init()完成后,字典knownOps包含以下内容:

knownOps    [String : Calculator.CalculatorModel.Op]    9 key/value pairs   
[0] _DictionaryElement<String, Calculator.CalculatorModel.Op>       
key String  "×" 
value   Calculator.CalculatorModel.Op   Operand Operand
[1] _DictionaryElement<String, Calculator.CalculatorModel.Op>       
key String  "+" 
value   Calculator.CalculatorModel.Op   Operand Operand
[2] _DictionaryElement<String, Calculator.CalculatorModel.Op>       
key String  "÷" 
value   Calculator.CalculatorModel.Op   Operand Operand
[3] _DictionaryElement<String, Calculator.CalculatorModel.Op>       
key String  "π" 
value   Calculator.CalculatorModel.Op   Constant    Constant
[4] _DictionaryElement<String, Calculator.CalculatorModel.Op>       
key String  "-" 
value   Calculator.CalculatorModel.Op   Operand Operand
[5] _DictionaryElement<String, Calculator.CalculatorModel.Op>       
key String  "±" 
value   Calculator.CalculatorModel.Op   UnaryOperation  UnaryOperation
[6] _DictionaryElement<String, Calculator.CalculatorModel.Op>       
key String  "sin"   
value   Calculator.CalculatorModel.Op   UnaryOperation  UnaryOperation
[7] _DictionaryElement<String, Calculator.CalculatorModel.Op>       
key String  "√" 
value   Calculator.CalculatorModel.Op   UnaryOperation  UnaryOperation
[8] _DictionaryElement<String, Calculator.CalculatorModel.Op>       
key String  "cos"   
value   Calculator.CalculatorModel.Op   UnaryOperation  UnaryOperation

我的问题是:为什么binaryOperations被捕获为字典中的操作数?

最佳答案 我在你的枚举中添加了以下变量:

var type: String {
    get {
        switch self {
        case .Operand:
            return "Operand"
        case .Constant:
            return "Constant"
        case .Variable:
            return "Variable"
        case .UnaryOperation:
            return "UnaryOperation"
        case .BinaryOperation:
            return "BinaryOperation"
        }
    }
}

然后使用以下代码循环遍历dict:

for (key, value) in knownOps
{
    let valueType = value.type
    println("\(key) : \(valueType)")
}

这给了我输出:

× : BinaryOperation
+ : BinaryOperation
÷ : BinaryOperation
π : Constant
- : BinaryOperation
± : UniaryOperation
sin : UniaryOperation
√ : UniaryOperation
cos : UniaryOperation

所以我想你的问题的答案是它不是.我无法回答为什么调试器或用于检查值的任何内容不正确,但您发布的代码没有错.

点赞