【发布时间】:2015-02-19 18:05:31
【问题描述】:
我对 Swift 很陌生,但是通过关注Stanford iTunes U course 慢慢学习。我有一个关于在数组中存储和调用函数的问题。
我拥有的代码(如下)似乎正确地存储了函数,但是当我尝试调用其中一个函数时出现此错误:'(IntegerLiteralConvertible, IntegerLiteralConvertible) -> $T6' is not identical to (String, Op)。
我发现 this answer 有助于我到达现在的位置,但现在我被困住了。
enum Op {
case binary((Double, Double) -> Double)
}
var ops = [String: Op]()
func addOperation(symbol: String, op: Op) {
ops[symbol] = op
}
addOperation("×", Op.binary(*))
addOperation("÷", Op.binary({ $1 / $0 }))
addOperation("+", Op.binary(+))
addOperation("−", Op.binary({ $1 - $0 }))
var newValue = ops["÷"](6, 3) // Produces the error
我的理解是ops["÷"]应该是我之前存储在数组中的函数。我没有正确调用它吗?
【问题讨论】:
标签: swift