正常reduce通话:
[1,2,3].reduce(0, { cur, val in
return val
})
Run Code Online (Sandbox Code Playgroud)
reduce尝试从以下位置调用EnumeratedSequence<Array<Element>>:
[1,2,3].enumerated().reduce(0, { cur, (index, element) in
return element
})
// Error: consecutive statements on a line must be separated by ';'" (at initial reduce closure)
Run Code Online (Sandbox Code Playgroud) 有没有一种方法可以使用.enumerated()和stride来通过索引大于1的字符串数组使用for-in循环,以保持索引和值?
例如,如果我有数组
var testArray2:[String] = [“ a”,“ b”,“ c”,“ d”,“ e”]
我想通过使用testArray2.enumerated()并遍历2来输出:
0, a
2, c
4, e
Run Code Online (Sandbox Code Playgroud)
所以理想上是这样的;但是,此代码将不起作用:
for (index, str) in stride(from: 0, to: testArray2.count, by: 2){
print("position \(index) : \(str)")
}
Run Code Online (Sandbox Code Playgroud)