我试图从一个循环内的数组中获取一批3。我觉得Swift必须有更优雅的方式。在Swift中从数组中获取N个批次的N?
这是我到目前为止有:
for (index, item) in allItems.enumerate() {
var batch: [MyType] = []
if index < allItems.endIndex {
batch.append(allItems[index])
}
if index + 1 < allItems.endIndex {
batch.append(allItems[index + 1])
}
if index + 2 < allItems.endIndex {
batch.append(allItems[index + 2])
}
sendBatchSomewhere(batch)
}
任何更好,更安全的方式来实现抓一批?中间很容易,但当然处理开始和结束会有点棘手。任何Swifty的想法?
更新:
谢谢,这个精美的作品!这里的操场版本:
import Foundation
typealias MyType = (a: String, b: Int, c: Int)
let allItems1: [MyType] = []
let allItems2 = [
(a: "Item 1", b: 2, c: 3)
]
let allItems3 = [
(a: "Item 1", b: 2, c: 3),
(a: "Item 2", b: 4, c: 5),
(a: "Item 3", b: 6, c: 7),
(a: "Item 4", b: 8, c: 9),
(a: "Item 5", b: 10, c: 11),
(a: "Item 6", b: 12, c: 13),
(a: "Item 7", b: 14, c: 15),
(a: "Item 8", b: 16, c: 17),
(a: "Item 9", b: 18, c: 19),
(a: "Item 10", b: 20, c: 21),
(a: "Item 11", b: 22, c: 23)
]
let testItems = allItems3 // Change to allItems1, allItems2, allItems3, etc
let batchSize = 3
let output = testItems.indices.map { fromIndex -> [MyType] in
let toIndex = fromIndex.advancedBy(batchSize, limit: testItems.endIndex)
return Array(testItems[fromIndex ..< toIndex])
}
print(output) =>
[
[("Item 1", 2, 3), ("Item 2", 4, 5), ("Item 3", 6, 7)],
[("Item 2", 4, 5), ("Item 3", 6, 7), ("Item 4", 8, 9)],
[("Item 3", 6, 7), ("Item 4", 8, 9), ("Item 5", 10, 11)],
[("Item 4", 8, 9), ("Item 5", 10, 11), ("Item 6", 12, 13)],
[("Item 5", 10, 11), ("Item 6", 12, 13), ("Item 7", 14, 15)],
[("Item 6", 12, 13), ("Item 7", 14, 15), ("Item 8", 16, 17)],
[("Item 7", 14, 15), ("Item 8", 16, 17), ("Item 9", 18, 19)],
[("Item 8", 16, 17), ("Item 9", 18, 19), ("Item 10", 20, 21)],
[("Item 9", 18, 19), ("Item 10", 20, 21), ("Item 11", 22, 23)],
[("Item 10", 20, 21), ("Item 11", 22, 23)],
[("Item 11", 22, 23)]
]
是不是故意的,你的“批次”交叠? –
好点,其实。建立手表并发症,因此批次中的第一项是“父母”。然后父母+ 1将成为下一批的父母。 – TruMan1