这是我的代码:
var algorithmResult = algorithm(value: value)
func rowCheck(#value: Int) -> (location: String, pattern: String)? {
var acceptableFinds = ["011", "101", "110"]
var findFunc = [checkTop, checkBottom, checkMiddleAcross, checkRight, checkMiddleDown, checkLeft, checkDiagLeftRight, checkDiagRightLeft]
for algorithm in findFunc {
var algorithmResult = algorithm(value: value)
if (find(acceptableFinds, algorithmResult.pattern) != nil) {
return algorithmResult
}
}
return nil
}
答案 0 :(得分:1)
在:
var algorithmResult = algorithm(value: value)
algorithm
代表findFunc
数组中的一个元素(如for algorithm in findFunc
中所定义)。
从名称来看,我猜这些元素中的每一个都是一个函数。这些函数传递value
,函数的结果存储在algorithmResult
。
这是一个类似的例子。创建两个函数:
func add(operand : Int) -> Int {
return operand + operand
}
func multiply(operand : Int) -> Int {
return operand * operand
}
将它们存储在一个数组中:
let funcs = [add, multiply]
循环调用它们:
for function in funcs {
let x = function(5)
print(x)
}
打印:
10
25
答案 1 :(得分:0)
它将findFunc
数组中的每个函数应用于传递给rowCheck
函数的值。