[SWIFT]enumerated()

힐링힐링·2023년 10월 22일
0

SWIFT 문법

목록 보기
22/26

enumerated()란 ?

배열 또는 컬렉션의 요소와 해당 요소의 인덱스를 반복하는 데 사용합니다. 이 함수는 "for-in" 루프와 함께 사용하여 요소와 인덱스를 각각 반환합니다

배열 예제

let fruits = ["apple", "banana", "cherry"]

for (index, fruit) in fruits.enumerated() {
    print("Index \(index): \(fruit)")
}

결과

Index 0: apple
Index 1: banana
Index 2: cherry

String 예제

let example = "ABCDEFG"

for (index, item) in example.enumerated(){
    print(index,item)
//    print(type(of: index))
}

결과,

0 A
1 B
2 C
3 D
4 E
5 F
6 G
profile
블로그 이전합니다 https://james-kim-tech.tistory.com/

0개의 댓글