swift seg fault错误新增到iOS开发

时间:2017-09-25 01:06:21

标签: ios swift

在构建时立即失败。有小费吗?我是Swift的新手,刚开始今天。

不确定seg故障发生的位置。

遵循斯坦福大学用Swift开发iOS 10应用程序

  

分段错误:在/Users/x/Documents/calculator/calculator/CalculatorBrain.swift:36:14为'performOperation'发出SIL时

import Foundation

struct CalculatorBrain {

    private var accumulator: Double?

    private enum Operation {
        case constant(Double)
        case unaryOperation((Double) -> Double)
        case binaryOperation((Double,Double) -> Double)
        case equals
    }

    private var operations: Dictionary<String, Operation> = [
            "π" : Operation.constant(Double.pi),
            "e" : Operation.constant(M_E),
            "√" : Operation.unaryOperation(sqrt), //sqrt,
            "cos" : Operation.unaryOperation(cos),//cos
            "±" : Operation.unaryOperation({-$0}),
            "x" : Operation.binaryOperation({$0*$1}),
            "+" : Operation.binaryOperation({$0+$1}),
            "-" : Operation.binaryOperation({$0-$1}),
            "÷" : Operation.binaryOperation({$0/$1}),
            "=" : Operation.equals
    ]

    mutating func performOperation(_ symbol: String) {
        if let operation = operations[symbol] {
            switch operation {
            case .constant(let value):
                accumulator = value
            case .unaryOperation(let function):
                if accumulator != nil{
                    accumulator = function(accumulator!)
                }
            case .binaryOperation(let function):
                if accumulator != nil {
                    PendingBinaryOperation = PendingBinaryOperation(function: function, firstOperand: accumulator!)
                    accumulator = nil
                }
            case .equals:
                performPendingBinaryOperation()
            }
        }
    }


    private mutating func performPendingBinaryOperation() {
        if PendingBinaryOperation != nil && accumulator != nil {
            accumulator = PendingBinaryOperation!.perform(with: accumulator!)
            PendingBinaryOperation = nil
        }
    }

    private var PendingBinaryOperation: PendingBinaryOperation?

    private struct PendingBinaryOperation {
        let function: (Double, Double) -> Double
        let firstOperand: Double

        func perform(with secondOperand: Double) -> Double {
            return function(firstOperand, secondOperand)
        }
    }

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

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

0 个答案:

没有答案