session-ios/SignalUtilitiesKit/Utilities/Collection+OWS.swift

21 lines
548 B
Swift

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
public extension Collection {
/// Returns the element at the specified index iff it is within bounds, otherwise nil.
subscript (safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}
public extension Array {
func chunked(by chunkSize: Int) -> [[Element]] {
return stride(from: 0, to: self.count, by: chunkSize).map {
Array(self[$0..<Swift.min($0 + chunkSize, self.count)])
}
}
}