案例1
for index in 0..<array.count {
guard let item = array[index] as? XXX else {
continue
}
// do other
}
优化后
for (index, header) in array.enumerated() {
guard let item = header as? XXX else {
continue
}
// do other
}
案例2
优化前
var index = 0
var tempItems: [[ObjectProtocol]] = [ObjectProtocol]
for item in self.items {
if index >= 3 {
break
}
if let f = self.items.last as? [ObjectProtocol] {
tempItems.append(f)
}
index += 1
}
self.items = tempItems
优化后
var tempItems: [[ObjectProtocol]] = []
for item in self.items.prefix(3) {
if let f = item as? [ObjectProtocol] {
tempItems.append(f)
}
}
self.items = tempItems