【问题标题】:Swift3 compile errorSwift3 编译错误
【发布时间】:2017-01-09 16:01:10
【问题描述】:

我是一名学习 Swift3 的学生。我目前正在用 Swift3 制作一个计算器,但我正在留下一个问题并提出问题。 请告诉我为什么该区域出现错误以及如何解决它。 错误消息:调用中的参数 #2 缺少参数 部分:第 20 行

import Foundation

func multiply(op1 :Double, op2: Double) -> Double {
    return op1 * op2
}

class CalculatorBrain {

    private var accumulator = 0.0

    func setOperand(operand: Double) {
        accumulator = operand
    }

    var operations: Dictionary<String, Operation> = [
        "π": Operation.Constant(M_PI),
        "e": Operation.Constant(M_E),
        "√": Operation.UnaryOperation(sqrt),
        "cos": Operation.UnaryOperation(cos),
        "×": Operation.BinaryOperation(multiply),
        "=": Operation.Equals
    ]

    enum Operation {
        case Constant(Double)
        case UnaryOperation((Double) -> Double)
        case BinaryOperation((Double), (Double) -> Double)
        case Equals
    }

    func performOperation(symbol: String) {
        if let operation = operations[symbol]{
            switch operation {
            case.Constant(let value): accumulator = value
            case.UnaryOperation(let function): accumulator = function(accumulator)
        case.BinaryOperation(let function): pending = PendingBinaryOperationInfo(binaryFunction: function, firstOperand:     accumulator)
            case.Equals:
                if pending != nil {
                    accumulator = pending!.binaryFunction(pending!.firstOperand, accumulator)
                    pending = nil
                }
            }
        }
    }

    private var pending: PendingBinaryOperationInfo?

    struct PendingBinaryOperationInfo {
        var binaryFunction: (Double, Double) -> Double
        var firstOperand: Double
    }

    var result: Double {
            get {
                return accumulator
            }
        }
}

【问题讨论】:

    标签: swift3


    【解决方案1】:

    不确定上下文是什么,但您可能想要更改

    enum Operation {
        case Constant(Double)
        case UnaryOperation((Double) -> Double)
        case BinaryOperation((Double), (Double) -> Double)
        case Equals
    }
    

    enum Operation {
        case Constant(Double)
        case UnaryOperation((Double) -> Double)
        case BinaryOperation((Double, Double) -> Double) // see change on this line
        case Equals
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      相关资源
      最近更新 更多