1、按数组的某个元素、某个元素属性进行分组:
//官方eg:
let students = ["Kofi", "Abena", "Efua", "Kweku", "Akosua"]
let studentsByLetter = Dictionary(grouping: students, by: { $0.first! })
// ["E": ["Efua"], "K": ["Kofi", "Kweku"], "A": ["Abena", "Akosua"]]
2、数组元素每2个为一组分段:
// 2个为一组
let arr = ["a", "b", "c", "d", "e"]
let step = 2
let finalArray = stride(from: 0, to: arr.count, by: step).map { (index) -> [String] in
if (index+step) > arr.count {
return Array(arr[index...])
} else {
return Array(arr[index..<index+step])
}
}