let a = ["a", "b", "c", "d", "e"]a.indices //=> 0..<5
let aSlice = a[1..<4] //=> ArraySlice with ["b", "c", "d"]aSlice.indices //=> 1..<4
var test = [Int: String]()for (index, element) in aSlice.enumerate() {test[index] = element}test //=> [0: "b", 1: "c", 2: "d"] // indices presented as 0..<3, but they are actually 1..<4test[0] == aSlice[0] // ERROR: out of bounds
var someStrs = [String]()
someStrs.append("Apple")someStrs.append("Amazon")someStrs += ["Google"]
for (index, item) in someStrs.enumerated(){print("Value at index = \(index) is \(item)").}
控制台:
Value at index = 0 is AppleValue at index = 1 is AmazonValue at index = 2 is Google
let list = ["100","200","300","400","500"]
// You can iterate the index and its elements using a closurelist.dropFirst(2).indexedElements {print("Index:", $0.index, "Element:", $0.element)}
// or using a for loopfor (index, element) in list.indexedElements {print("Index:", index, "Element:", element)}