Swift:基本语法

Swift:基本语法,swift,syntax,enums,case,Swift,Syntax,Enums,Case,我正在尝试使用MVC版本的Swift制作一个计算器。有一次我请了一些人来帮助我,他们给了我一些我不太懂的代码,然后是枚举部分,特别是嵌套在枚举中的案例。 换句话说,我们的意思是什么 case UnaryOperation (String,Double -> Double) case BinaryOperation(String,(Double,Double)-> Double) 第一部分接收参数之一是字符串很有意义,但第二部分让我很困惑 class CalculatorBrain

我正在尝试使用MVC版本的Swift制作一个计算器。有一次我请了一些人来帮助我,他们给了我一些我不太懂的代码,然后是枚举部分,特别是嵌套在枚举中的案例。 换句话说,我们的意思是什么

case UnaryOperation (String,Double -> Double)
case BinaryOperation(String,(Double,Double)-> Double)
第一部分接收参数之一是
字符串
很有意义,但第二部分让我很困惑

class CalculatorBrain {

    enum Op{

        case Operand (Double)
        case UnaryOperation (String,Double -> Double)
        case BinaryOperation(String,(Double,Double)-> Double)

    }

    var opStack = [Op]()
    func pushOperand(Operand : Double){
        opStack.append(Op.operand(Operand))
    }

}
考虑到这两个方面:

case UnaryOperation (String, Double -> Double)
case BinaryOperation (String, (Double,Double) -> Double)
这只是意味着
UnaryOperation
有两个“关联值”,一个是
字符串
,另一个是闭包(一个以
Double
作为参数并返回
Double
)。
BinaryOperation
是相同的,只是它的闭包需要两个参数,两个
Double


请参阅Swift编程语言中的(特别是“关联值”部分)和章节。

查看AppleBTW提供的免费Swift书籍,
append
不正确。您可能需要这样的内容:
func-pushOperand(操作数:Double){opStack.append(Op.operand(操作数))}