Split large Array in Array of two elements

前端 未结 7 2080
借酒劲吻你
借酒劲吻你 2020-12-20 17:53

I have large list of objects and I need to split them in a group of two elements for UI propouse.

Example:

[0, 1, 2, 3, 4, 5, 6]

Becomes

7条回答
  •  借酒劲吻你
    2020-12-20 18:25

    The shortest solution (Swift 4), I have seen so far, is from a Gist:

    extension Array {
    
        func chunks(chunkSize: Int) -> [[Element]] {
            return stride(from: 0, to: self.count, by: chunkSize).map {
                Array(self[$0..

提交回复
热议问题