Link
Notice
HIT해
[Swift 기초문법 - 55] Dictionary 반복 본문
728x90
Swift Dictionary 를 반복하는 방법을 알아보자.
- for in
- foreacc
- keys, values
사용예시
1. for in
let myFriends : [String: Int] = ["철수" : 19, "수잔": 23, "제임스": 30]
for (name, age) in myFriends {
print("이름: \(name), 나이: \(age)")
}
2. forEach
myFriends.forEach { (name: String, age: Int) in
print("이름: \(name), 나이: \(age)")
}
3. keys, values
myFriends.keys.forEach { name in
print("이름: \(name)")
}
myFriends.values.forEach { age in
print("나이: \(age)")
}
'Swift > 기초문법' 카테고리의 다른 글
[Swift 기초문법 - 58] Any, AnyObject, some (0) | 2024.08.19 |
---|---|
[Swift 기초문법 - 56] Set 내장함수 (0) | 2024.08.19 |
[Swift 기초문법 - 54] stride (0) | 2024.08.19 |
[Swift 기초문법 - 53] Subset Superset (0) | 2024.08.19 |
[Swift 기초문법 - 52] Optional Protocol (0) | 2024.08.19 |