📍 여러 값들을 묶어 하나의 변수로 표현할 수 있도록 한다
📍 순서가 있는 리스트 컬렉션이다
📍 Array<Type>
과 [Type]
는 동일하다
📍 빈 배열을 선언할 때는 []
만 적어도 된다
// 빈 Array 생성
var integers: Array<Int> = Array<Int>()
var doubles: Array<Double> = [Double]()
var strings: [String] = [String]()
var characters: [Character] = []
📍 let
을 사용하는 경우 불변 배열이 된다
📍 삽입, 수정, 삭제가 불가능하다
📍 대괄호로 임의 접근이 가능하다
📍 범위를 벗어나는 경우 오류가 발생한다
integers.append(1)
integers.append(100)
📍 명시된 자료형이 아닌 다른 자료형의 변수를 넣는 경우 오류가 발생한다
📍 remove(at: idx)
: 해당 인덱스의 요소를 제거한다
📍 removeLast()
: 맨 마지막 요소를 제거한다
📍 removeAll()
: 배열 내 모든 요소를 제거한다
integers.remove(at: 0)
integers.removeLast()
integers.removeAll()
integers.contains(100)
integers.contains(99)
integers.count
📍 key
와 value
의 쌍으로 이루어진 컬렉션이다(해시맵과 유사)
📍 Dictionary<Type1, Type2>
와 [Type1: Type2]()
는 같다
📍 let
으로 선언하는 경우 불변 딕셔너리이다
// 빈 딕셔너리 선언
var dictionary: Dictionary<String: Any> = [String: Any]()
var dict: [Type1: Type2] = [:]
// 불변 딕셔너리
let immutableDict: [Type1: Type2] = [key1: val1, key2: val2]
dictionary[newKey] = newValue
📍 이미 존재하는 키를 사용하는 경우 해당하는 키에 대응되는 value
는 새로운 value
로 변경된다
📍 removeValue(forKey: key)
: 해당하는 key
의 값을 제거한다
📍 removeAll()
: 모든 요소를 제거한다
📍 제거하고자 하는 키의 값에 nil
을 할당해도 된다
dictionary.removeValue(forKey: key)
dictionary[key] = nil
📍 순서가 없고 멤버가 유일한 컬렉션이다(집합)