티스토리 뷰

Swift/문법

Swift Dictionary

지용빡 2022. 4. 21. 23:29
반응형
Swift Dictionary 정리
Dictionary란?
  • 데이터를 키-값(key & value)  쌍의 형태로 저장하고 관리
  • 배열과 비슷한 작업의 목적의 작업을 하지만 순서가 없다.
  • Dictionary에 저장된 각 항목은 값을 참조하고 접근하는데 사용되는 유일한 키와 연결되어 있다.
  • 키는 해시 가능한 타입이어야 한다
  • Swift의 기본 타입은 해쉬 가능
Swift의 Dictionary는 generic 구조체
var x = [Int:String]()
var y : Dictionary<String,String> = [:]
var a1 : [Int:String] = [1:"일", 2:"이", 3:"삼"]
var a2 : Dictionary<Int,String> = [1:"일", 2:"이", 3:"삼"] var a3 = [1:"일", 2:"이", 3:"삼"]
var b1 : [String:Double] = ["김보통":60.5,"이갈비":42.7,"엄청군":123.4]
var b2 : Dictionary<String,Double> = ["김보통":60.5,"이갈비":42.7,"엄청군":123.4] var b3 = ["김보통":60.5,"이갈비":42.7,"엄청군":123.4]
let colors = ["빨강":"red", "초록":"green", "파랑":"blue"]
Dictionary의 자료형
var a1 : [Int:String] = [1:"일", 2:"이", 3:"삼"]
var a2 : Dictionary<Int,String> = [1:"일", 2:"이", 3:"삼"]
var a3 = [1:"일", 2:"이", 3:"삼"]
print(type(of:a1)) //Dictionary<Int, String>
print(type(of:a2)) //Dictionary<Int, String>
print(type(of:a3)) //Dictionary<Int, String>
var b1 : [String:Double] = ["김보통":60.5,"이갈비":42.7,"엄청군":123.4]
var b2 : Dictionary<String,Double> = ["김보통":60.5,"이갈비":42.7,"엄청군":123.4]
var b3 = ["김보통":60.5,"이갈비":42.7,"엄청군":123.4]
print(type(of:b3)) //Dictionary<String, Double>
let colors = ["빨강":"red", "초록":"green", "파랑":"blue"]
print(type(of:colors)) //Dictionary<String, String>

* 참고 문헌 : 2022 iOS 프로그래밍 실무 한성현 교수님 강의 내용 중 변형 

반응형

'Swift > 문법' 카테고리의 다른 글

Swift final class 알아보기  (0) 2022.06.19
Swift String(문자열) 쉽게 다루기  (0) 2022.06.14
Swift 옵셔널 체이닝(Optional Chaining)  (0) 2022.02.14
Swift 에러 처리  (0) 2022.02.10
Swift 고차함수  (0) 2022.02.09
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함