for语句中的这行代码是什么意思或做什么?

时间:2015-08-02 20:26:09

标签: ios iphone swift for-loop tuples

我正在学习游戏应用程序的教程,并且有一行代码我不明白它看起来像是类型元组

这是我的代码:

 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
    }

2 个答案:

答案 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函数的值。