我得到一个未解析的标识符,即使我有一个let变量

时间:2017-06-21 00:45:20

标签: swift

我在PendingBinaryOperation结构中设置了常量calculationError 我创建了一个PendingBinaryOperation类型的变量pendingBinaryOperation 我在case语句中得到错误,即calculateError是一个未解析的标识符。代码部分如下。

mutating func performOperation(_ symbol:String){
    if let currentOperation = operations[symbol] {
        switch currentOperation {
        case .nullOperation(let function, let nullDiscription):
            accumulator = (function(), nullDiscription, nil)
        case .constant(let value):
            accumulator = (value,symbol, nil)
        case .unaryOperation(let function, let unaryDescription, let unaryMemory):
            if accumulator != nil {
                accumulator = (function(accumulator!.digit), unaryDescription(accumulator!.description),
                unaryMemory(accumulator!.digit))
            }
        case .binaryOperation(let binaryFunction,
                              let binaryDescription,
                              let binaryError):
            if accumulator != nil {
                doAPendingBinaryOperation()
                resultIsPending = true
                pendingBinaryOperation = PendingBinaryOperation(
                    mathFunction: binaryFunction,
                    calculationDescription:binaryDescription,
                    firstOperand: accumulator!.digit,
                    descriptionOperand:(accumulator?.description)!,
 THIS LINE GIVES THE ERROR ->binaryError: calculationError) 
                accumulator = nil
            }
        case .equals:
            doAPendingBinaryOperation()
            resultIsPending = false
        }
    }
}

private var pendingBinaryOperation:PendingBinaryOperation?

struct PendingBinaryOperation {
    let calculationError: (Double, Double) -> String
    let mathFunction: (Double, Double) -> Double
    let calculationDescription: (String, String) -> String
    let firstOperand: (Double)
    let descriptionOperand:(String)
    func perform(with secondOperand:(Double)) -> Double {
        return (mathFunction(firstOperand, secondOperand))
    }
    func performDecription(with secondOperan:(String)) -> String {
        return (calculationDescription(descriptionOperand, secondOperan))
    }
}

2 个答案:

答案 0 :(得分:0)

Swift中的结构会自动生成一个成员初始化程序,这就是你在这里用来初始化PendingBinaryOperation的内容。这要求在初始化程序中设置所有属性,包括binaryError

您尝试将calculationError作为binaryError参数的值传递 - 这是不可能的,因为calculationError在此范围内不可用(并且没有值)。

如果您不想设置binaryError,则必须放弃使用memberwise初始值设定项。这通常意味着添加PendingBinaryOperation属性的初始值,并使用正常的init(即PendingBinaryOperation())。

答案 1 :(得分:0)

根据您显示的代码,PendingBinaryOperation的构造函数为:

PendingBinaryOperation(calculationError: (Double, Double) -> String, mathFunction: (Double, Double) -> Double, calculationDescription: (String, String) -> String, firstOperand: (Double), descriptionOperand: (String))

但你在打电话:

PendingBinaryOperation(mathFunction: (Double, Double) -> Double, calculationDescription: (String, String) -> String, firstOperand: (Double), descriptionOperand:(String), binaryError: ??)

正如您所看到的,参数的顺序都是错误的,并且您有一个名为binaryError的参数,该参数在该类型中不存在。

我猜您需要将binaryError: calculationError转回calculationError: binaryError并重新排序您的参数以使它们正确无误。