1

正常reduce通话:

[1,2,3].reduce(0, { cur, val in
  return val
})

尝试从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)
4

1 回答 1

4

您可以使用和with访问element元组的:val.elementindexval.offset

let result = [1,2,3].enumerated().reduce(0, { cur, val in
    return val.element
})

或者,您可以使用赋值来访问元组中的值:

let result = [1,2,3].enumerated().reduce(0, { cur, val in
    let (index, element) = val
    return element
})
于 2017-04-03T13:56:33.533 回答