【问题标题】:what does this line of code in the for statement mean or do?for 语句中的这行代码是什么意思或做什么?
【发布时间】:2015-08-02 20:26:09
【问题描述】:

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

这是我的代码:

 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
    }

【问题讨论】:

    标签: ios iphone swift for-loop tuples


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      它将 findFunc 数组中的每个函数应用于传递给 rowCheck 函数的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-22
        • 2014-11-27
        • 2011-07-12
        • 1970-01-01
        相关资源
        最近更新 更多