Swift : 基本语法
Swift : Basic syntax
我正在尝试使用 MVC 版本的 Swift 制作一个计算器。有一次我请某人帮助我,他们给了我这段我不太理解的代码,然后是枚举部分,特别是枚举中嵌套的情况。
换句话说,
是什么意思
case UnaryOperation (String,Double -> Double)
case BinaryOperation(String,(Double,Double)-> Double)
第一部分,其中一个接收参数是一个 String
很有道理,但第二部分让我很困惑
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
有两个 "associated values",其中一个是 String
,另一个是闭包(一个将 Double
作为参数和 returns 一个 Double
)。 BinaryOperation
是一样的,除了它的闭包有两个参数,两个 Double
.
请参阅Swift编程语言中的Enumerations (notably the "Associated Values" section) and Closures章节。
我正在尝试使用 MVC 版本的 Swift 制作一个计算器。有一次我请某人帮助我,他们给了我这段我不太理解的代码,然后是枚举部分,特别是枚举中嵌套的情况。 换句话说,
是什么意思case UnaryOperation (String,Double -> Double)
case BinaryOperation(String,(Double,Double)-> Double)
第一部分,其中一个接收参数是一个 String
很有道理,但第二部分让我很困惑
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
有两个 "associated values",其中一个是 String
,另一个是闭包(一个将 Double
作为参数和 returns 一个 Double
)。 BinaryOperation
是一样的,除了它的闭包有两个参数,两个 Double
.
请参阅Swift编程语言中的Enumerations (notably the "Associated Values" section) and Closures章节。