ForEachでインデックスを使うには、enumerated()
を使います。
let array = ["りんご", "みかん", "レモン", "スイカ"]
array.enumerated().forEach { index, string in
print("\(index): \(string)")
}
// 0: りんご
// 1: みかん
// 2: レモン
// 3: スイカ
let array = ["りんご", "みかん", "レモン", "スイカ"]
array.enumerated().forEach { index, string in
print("\(index): \(string)")
}
// 0: りんご
// 1: みかん
// 2: レモン
// 3: スイカ
let array = ["りんご", "みかん", "レモン", "スイカ"] array.enumerated().forEach { index, string in print("\(index): \(string)") } // 0: りんご // 1: みかん // 2: レモン // 3: スイカ