Caps-iOS/CapCollector/Extensions/Array+Extensions.swift

31 lines
758 B
Swift
Raw Normal View History

2020-05-16 11:21:55 +02:00
//
// Array+Extensions.swift
// CapCollector
//
// Created by Christoph on 12.05.20.
// Copyright © 2020 CH. All rights reserved.
//
import Foundation
extension Array {
func split(intoPartsOf maxElements: Int) -> [ArraySlice<Element>] {
guard !isEmpty, maxElements > 0 else {
return []
}
var result = [ArraySlice<Element>]()
var currentIndex = 0
while true {
let nextIndex = currentIndex + maxElements
if nextIndex >= count {
result.append(self[currentIndex..<count])
return result
}
result.append(self[currentIndex..<nextIndex])
currentIndex += maxElements
}
return result
}
}