Swift - Enum Returns the wrong type of value.

I am having problems with the following code:

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 })
}

After init () completes, the dictionary knownOpscontains the following:

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

My question is: why is it binaryOperationwritten like Operandin a dictionary?

+4
source share
2 answers

I have added the following variable to your enumeration:

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"
        }
    }
}

Then looped through the dict with the following code:

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

This gave me the result:

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

So, I think the answer to your questions is that it is not. I cannot give an answer about why the debugger or anything else that you used to check the values ​​was incorrect, but the code you posted is not incorrect.

+1
source

, , , . Xcode 6.4, knownOps, , .

, Swift .

, :

  • ( ) Xcode 7 beta enum knownOps Invalid.
  • "" knownOps, (, for-loop), , , Xcode 6.4, - Xcode 7.
  • , knownOps, , : 1) , enum, 2 ) , , description enum.

, , , "", , "" . , , 6.4 .

, Xcode 7 "" . Invalid , .

+1

Source: https://habr.com/ru/post/1598068/


All Articles