컬렉션 타입
- Array
- 순서(인덱스)가 있는 멤버를 가진 리스트 형태의 컬렉션 타입
var integersArray: Array<Int> = Array<Int>()
var integersArray: Array<Int> = []
integersArray.append(3)
integersArray.append(10)
integersArray.contains(3)
integersArray.contains(5)
integersArray[0] = 7
integersArray.count
integersArray.remove(at: 0)
integersArray.removeLast()
integersArray.removeAll()
let immutableArray = [1, 2, 3]
- Dictionary
- 키(key)와 값(value)의 쌍으로 이루어진 컬렉션 타입
var englishDictionary: Dictionary<String, String> = Dictionary<String, String>()
- 메소드 등 여러가지 형태로 Dictionary 활용
var englishDictionary: Dictionary<String, String> = [:]
englishDictionary["elephant"] = "코끼리"
englishDictionary["lion"] = "사자"
englishDictionary["elephant"] = "코끼리"
englishDictionary["lion"] = "사자"
englishDictionary["lion"] = "호랑이"
englishDictionary.removeValue(forKey: "lion")
englishDictionary["elephant"] = nil
let initalizedDictionary: [String: String] = ["A": "a", "B": "b"]
let someValue: String = initalizedDictionary["A"]
- Set
- 순서가 없고, 중복된 멤버가 없는 컬렉션 타입
- 집합과 비슷한 개념
var integersSet: Set<Int> = Set<Int>()
integersSet.insert(2)
integersSet.insert(4)
integersSet.insert(6)
integersSet.insert(6)
integersSet.contains(2)
integersSet.contains(3)
integersSet.remove(4)
integersSet.removeFirst()
integersSet.count
let setA: Set<Int> = [1, 2, 3, 4, 5]
let setB: Set<Int> = [2, 4, 6 ,8, 10]
let union: Set<Int> = setA.union(setB)
let sortedUnion: [Int] = union.sorted()
let intersection: Set<Int> = setA.intersection(setB)
let subtractiong: Set<Int> = setA.subtracting(setB)