for语句中的这行代码是什么意思或做什么?
what does this line of code in the for statement mean or do?
我正在学习一个游戏应用程序的教程,有一行代码我不明白它看起来像是元组类型
这是我的代码:
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
}
它将 findFunc
数组中的每个函数应用于传递给 rowCheck
函数的值。
在:
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
我正在学习一个游戏应用程序的教程,有一行代码我不明白它看起来像是元组类型
这是我的代码:
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
}
它将 findFunc
数组中的每个函数应用于传递给 rowCheck
函数的值。
在:
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