* Array - 순서가 있는 리스트 컬렉션
*빈 Int Array 생성
ex)
var integers: Array<Int> = Array<Int>()
//요소를 맨 뒤에 추가하고 싶으면 append를 쓰면 된다.
integers.append(1)
integers.append(100)
//integers.append(101.1)
//이미 Array를 생성할때 Int값을 지정해줘서 다른 타입을 추가하는 것은 안된다.
print(integers) // [1, 100]
* contains - 이러한 메소드를 가지고있는지 true, false로 나타낸다.
ex)
print(integers.contains(100)) // true
print(integers.contains(99)) // false
integers.remove(at: 0)
//0번째 있는 값을 없애고 싶다고 지정한 것
integers.removeLast()
//맨 마시막 요소를 없애는 것
integers.removeAll()
//모든 요소를 없애는 것
print(integers.count) // 0
//count로 어떤 요소값들이 들어있는지를 확인하는 것
//integers[0] // 범위 초과 - 런타임 오류 발생
//0번째의 값을 이미 지웠기때문에 범위초과가 되므로 오류가 발생한다.
* Array<Double>와 [Double]는 동일한 표현
*빈 Double Array 생성
var doubles: Array<Double> = [Double]()
* 빈 String Array 생성
var strings: [String] = [String]()
* 빈 Character Array 생성
* []는 새로운 빈 Array
var characters: [Character] = []
* let을 사용하여 Array를 선언하면 불변 Array
let immutableArray = [1, 2, 3] //미리 요소를 정해서 만든것
// 불면 Array의 요소는 추가/삭제 불가 - 컴파일 오류 발생
//immutableArray.append(4)
//immutableArray.removeAll()
//위에 let으로 불변객체가 되었으므로 오류가 발생한다.
* Dictionary - 키와 값이 쌍으로 이루어진 컬렉션
* Key가 String 타입이고 Value가 Any인 빈 Dictionary 생성
var anyDictionary: Dictionary<String, Any> = [String: Any]()
anyDictionary["someKey"] = "value"
anyDictionary["anotherKey"] = 100
print(anyDictionary) // ["someKey": "value", "anotherKey": 100]
//가다나 순으로 나오는것도 아닌 키와 값을 쌍으로 나타나기때문 순서는 상관없다.
* Key에 해당하는 값 변경
anyDictionary["someKey"] = "dictionary"
print(anyDictionary) // ["someKey": "dictionary", "anotherKey": 100]
* Key에 해당하는 값 제거
anyDictionary.removeValue(forKey: "anotherKey")
anyDictionary["someKey"] = nil
print(anyDictionary) // [:]
* 빈 Dictionary 생성
let emptyDictionary: [String: String] = [:]
* 초기 값을 가지는 Dictionary 생성
let initalizedDictionary: [String: String] = ["name": "yagom", "gender": "male"]
// let으로 선언한 불변 Dictionary는 수정 불가 - 컴파일 오류 발생
//emptyDictionary["key"] = "value"
// name 키에 해당하는 값이 Dictionary에 존재하지 않을 수 있으므로
// 컴파일 오류 발생 - 옵셔널 파트에서 상세히 다룹니다
//let someValue: String = initalizedDictionary["name"]
* Set - 순서가 없고, 멤버가 유일한 컬렉션
* 빈 Int Set 생성
var integerSet: Set<Int> = Set<Int>()
integerSet.insert(1)
integerSet.insert(100)
integerSet.insert(99)
integerSet.insert(99)
integerSet.insert(99)
print(integerSet) // [100, 99, 1]
//99를 3번 추가를 했지만 set는 중복된 값이 없다라는 것을 보장해주기때문에 중복되지않는다.
print(integerSet.contains(1)) // true
print(integerSet.contains(2)) // false
//2번째 배열에는 값이 들어가있지 않기때문에 거짓이 나온다.
integerSet.remove(100)
integerSet.removeFirst()
integerSet.count) // 1
// Set는 집합 연산에 꽤 유용합니다
let setA: Set<Int> = [1, 2, 3, 4, 5]
let setB: Set<Int> = [3, 4, 5, 6, 7]
// 합집합
let union: set<Int> = setA.union(setB)
print(union) // [2, 4, 5, 6, 7, 3, 1]
// 합집합 오름차순 정렬
let sortedUnion: [Int] = union.sorted()
print(sortedUnion) // [1, 2, 3, 4, 5, 6, 7]
// 교집합
let intersection: Set<Int> = setA.intersection(setB)
print(intecrsection) // [5, 3, 4]
// 차집합
let subtracting: Set<Int> = setA.subtracting(setB)
print(subtracting) // [2, 1]
관련문서: docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html
Collection Types — The Swift Programming Language (Swift 5.3)
Collection Types Swift provides three primary collection types, known as arrays, sets, and dictionaries, for storing collections of values. Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordere
docs.swift.org
출처 - yagom 스위프트 기본 문법 영상(5강), 위키백과
'IT > ▒ swift' 카테고리의 다른 글
Swift - 함수 고급 (0) | 2021.04.26 |
---|---|
Swift - 함수 기본 (0) | 2021.02.08 |
Swift - Any, AnyObject, nil (0) | 2021.02.07 |
Swift - 기본 데이터 타입 (0) | 2021.02.05 |
Swift - camel Case, 상수와 변수 (0) | 2021.02.05 |