1. ๊ฐ๋
[๊ฐ]์์ (์ธ๋ฑ์ค, ๊ฐ)์ ํํ ํ์์ ๋ฐฐ์ด์ด ๋ฆฌํด๋๋ค. Array์์ ์ฌ์ฉ๋๋ ๋ฉ์๋๋ก, ๋ฌธ์์ด ๊ฐ๋ฅ.
2. ์ฌ์ฉ
let text = "white"
let array = [3.4, 3.5235, 1.3, -0.4, -9.9]
for (index, value) in text.enumerated() {
print("\\(index): \\(value)")
}
//0: w
//1: h
//2: i
//3: t
//4: e
for number in array.enumerated() {
print("\\(number.offset): \\(number.element)")
}
//0: 3.4
//1: 3.5235
//2: 1.3
//3: -0.4
//4: -9.9
- index, value์ ๊ฐ๊ฐ ์์ผ๋ ์นด๋๋ฅผ ๋ฃ์ด์ ์๋ต ๊ฐ๋ฅํ๋ค. ๊ทธ๋ฌ๋ ๊ทธ๋ฌ๋ฉด enumerated๋ฅผ ์ฌ์ฉํ๋ ์๋ฏธ๊ฐ…………….?
- ํํ๋ก ๋๋์ด ์ฌ์ฉํ์ง ์๊ณ ํ๋์ ๋ณ์๋ก๋ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ฐ, ์ด๋ .offset์ ์ธ๋ฑ์ค, .element๋ ๊ฐ์ผ๋ก ์ ๊ทผํ ์ ์๋ค.
3. ์ฐธ๊ณ ์๋ฃ
'Swift ๊ธฐ๋ณธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] Result (0) | 2024.08.14 |
---|---|
[Swift] Hashable ํ๋กํ ์ฝ (0) | 2024.05.24 |
[Swift] ์ด๊ฑฐํ(Enumeration) (0) | 2024.02.26 |
[Swift] ํ์ ์บ์คํ (Type Casting) (0) | 2024.02.02 |