2017. 1. 5. 12:29ㆍProgramming/Swift
1.
var product = [String:[String]]() product = ["it":["phone","computer"],"school":["pen","note"]] product["it"]?.append("tablet") print(product) //["it": ["phone", "computer", "tablet"], "school": ["pen", "note"]] product.updateValue(["handle"], forKey: "car") print(product) //["it": ["phone", "computer", "tablet"], "car": ["handle"], "school": ["pen", "note"] |
2.
var product2 = [String:[String:Any]]() product2 = ["it":["phone":4],"school":["pen":2,"note":3]] product2["it"]?.updateValue(1, forKey: "computer") //it에 추가적으로 값을 넣는다. print(product2)
["it": ["phone": 4, "computer": 1], "school": ["pen": 2, "note": 3]] product2.updateValue(["android":1], forKey: "it") //it에 기존값을 제거하고 새롭게 넣는다.
print(product2)
["it": ["android": 1], "school": ["pen": 2, "note": 3]] product2.updateValue(["handle":1,"gear":1], forKey: "car") //car 값을 새롭게 넣는다.
print(product2)
["it": ["android": 1], "car": ["gear": 1, "handle": 1], "school": ["pen": 2, "note": 3]] |
3.
var product2 = [String:[String:Any]]() product2 = ["it": ["phone": 4, "computer": 1], "car": ["gear": 1, "handle": 1], "school": ["pen": 2, "note": 3]] for name in product2.keys { print("My first key is \(name)") //1번째 key값을 가져온다. //My first key is it //My first key is car //My first key is school if let second = product2[name] { for (key, value) in second { print("My Second key is \(key)") //2번째 key값을 가져온다. //My Second key is phone //My Second key is computer //My Second key is handle //My Second key is gear //My Second key is pen
//My Second key is note print("My value is \(value)") //value를 가져온다. //My value is 4 //My value is 1 //My value is 1 //My value is 1 //My value is 2
//My value is 3 } } } |
- 결과값
My first key is it My Second key is phone My value is 4 My Second key is computer My value is 1 My first key is car My Second key is handle My value is 1 My Second key is gear My value is 1 My first key is school My Second key is pen My value is 2 My Second key is note My value is 3 |
'Programming > Swift' 카테고리의 다른 글
[ERROR] Could not locate device support files. (0) | 2017.01.09 |
---|---|
AppStore 통계 관련 설명 URL (0) | 2017.01.06 |
swift3 한글url 처리 (0) | 2016.12.19 |
이미지는 알파 채널 또는 투명도를 포함할 수 없습니다 (0) | 2016.12.19 |
[ERROR] 바이너리가 iPad를 지원하지 않습니다. (0) | 2016.11.15 |