【发布时间】:2020-04-01 13:16:56
【问题描述】:
我正在使用 Swift 文档在 Swift 5.1 中学习递归枚举。
这是一个代码。
indirect enum ArithmeticExpression {
case number(Int)
case addition(ArithmeticExpression, ArithmeticExpression)
case multiplication(ArithmeticExpression, ArithmeticExpression)
func evaluate(_ expression: ArithmeticExpression) -> Int {
switch expression {
case let .number(value):
return value
case let .addition(left, right):
return evaluate(left) + evaluate(right)
case let .multiplication(left, right):
return evaluate(left) * evaluate(right)
}
}
}
let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))
print(ArithmeticExpression.evaluate(product))
我认为最后一行代码出了点问题。
这是什么意思?
【问题讨论】:
-
提示:了解静态(类型)方法和实例方法之间的区别。
-
控制台未打印
lldb_expr_5。控制台根据你的截图打印(Function)。 -
lldb_expr_5 是 Playground 正在构建您的代码的 swift 模块的名称。 Swift 代码必须存在于模块中,因此 Playground(和 REPL)必须为其命名,我们选择 lldb_expr... 5 是因为 Playground 中的每个“提交”代码都会编译成不同的模块。这是为了帮助我们处理重新定义,这在 Playground 和 REPL 中是有用的,但在单个模块中是不允许的。模块名称是类全名的一部分,这就是您在此输出中看到它的原因。它与您的问题无关。
标签: swift xcode enums console lldb