[Swift] enumerated()
μμΈ λ΄μ© : enumerated() | Apple Document
λ μμ΄λ€.. π μνΌ μ΄λ μμ λ°°μ΄ννμ Sequence λ°μ΄ν°λ₯Ό νμν λ κ° λ°μ΄ν°λ§λ€ (n, x) ννμ ννμ 리ν΄νλλ°, μ¬κΈ°μ nμ μΈλ±μ€, xλ ν΄λΉλλ μμμ κ°μ λνλΈλ€.
μλ₯Ό λ€μ΄μ λ€λ₯Έ Languageμμ μλμ²λΌ forλ¬Έμ ν΅ν΄ λ°°μ΄μ μννλλ°, (C++ λ무 μ€λλ§μ΄λΌ λ¬Έλ²λ λͺ¨λ₯΄κ² λ€ γ γ ; π©)
for (int i=0; i<array.size(); i++) {
cout << i << "= " << array[i] << endl;
}
Swiftμμλ μΈλ±μ€ λ²μμ ν¬κ² μ κ²½μ°μ§ μμλ λλ, λ°λ‘ λ°°μ΄μ μμκ°μ μ κ·Ό κ°λ₯νλλ‘ forλ¬Έμ ν΅ν κ°λ ₯ν Sequence νμκΈ°λ₯μ μ 곡νλ€. μνΌ.. enumeratedλ μλμ²λΌ μμ½κ² Sequenceλ₯Ό νμν μ μκ² λμμ£Όλ μΉκ΅¬λ€ ππ μ μ©ν©μλ€!
let array: [String] = ["my", "name", "is", "kt"]
for (n, x) in array.enumerated() {
print("\(n) = \(x)")
}
/*
0 = my
1 = name
2 = is
3 = kt
*/
let array2: [Int] = [11, 22, 33, 44, 55]
for (n, x) in array2.enumerated() {
print("\(n) = \(x)")
}
/*
0 = 11
1 = 22
2 = 33
3 = 44
4 = 55
*/