반응형
안녕하세요 공공돌🧸 입니다.
컬렉션 타입(Array, Dictionary, Set) 에 대해 정리 해봤습니다.
1. 컬렉션 타입
- Array - 순서가 있는 리스트 컬렉션
- Dictionary - '키'와 '값'의 쌍으로 이루어진 컬렉션
- Set - 순서가 없고, 멤버가 유일한 컬렉션
Array
- 멤버가 순서(인덱스)를 가진 리스트 형태의 컬렉션 타입
- 여러가지 리터럴 문법을 활용할 수 있어 표현 방법이 다양합니다.
// 1. Array 선언 및 생성
var integers: Array<Int> = Array<Int>()
// 위와 동일한 표현
// var integers: Array<Int> = [Int]()
// var integers: Array<Int> = []
// var integers: [Int] = Array<Int>()
// var integers: [Int] = [Int]()
// var integers: [Int] = []
// var integers = [Int]()
// 2. Array 활용
integers.append(1)
integers.append(100)
// Int 타입이 아니므로 Array에 추가할 수 없습니다
//integers.append(101.1)
print(integers) // [1, 100]
// 멤버 포함 여부 확인
print(integers.contains(100)) // true
print(integers.contains(99)) // false
// 멤버 교체
integers[0] = 99
// 멤버 삭제
integers.remove(at: 0)
integers.removeLast()
integers.removeAll()
// 멤버 수 확인
print(integers.count)
// 인덱스를 벗어나 접근하려면 익셉션 런타임 오류발생
//integers[0]
// 3. 불변 Array: let을 사용하여 Array 선언
let immutableArray = [1, 2, 3]
// 수정이 불가능한 Array이므로 멤버를 추가하거나 삭제할 수 없습니다
//immutableArray.append(4)
//immutableArray.removeAll()
Dictionary
- '키'와 '값'의 쌍으로 이루어진 컬렉션 타입
- Array와 비슷하게 여러가지 리터럴 문법을 활용할 수 있어 표현 방법이 다양합니다.
// 1. Dictionary의 선언과 생성
// Key가 String 타입이고 Value가 Any인 빈 Dictionary 생성
var anyDictionary: Dictionary<String, Any> = [String: Any]()
// 위와 동일한 표현
// var anyDictionary: Dictionary <String, Any> = Dictionary<String, Any>()
// var anyDictionary: Dictionary <String, Any> = [:]
// var anyDictionary: [String: Any] = Dictionary<String, Any>()
// var anyDictionary: [String: Any] = [String: Any]()
// var anyDictionary: [String: Any] = [:]
// var anyDictionary = [String: Any]()
// 2. Dictionary 활용
// 키에 해당하는 값 할당
anyDictionary["someKey"] = "value"
anyDictionary["anotherKey"] = 100
print(anyDictionary) // ["someKey": "value", "anotherKey": 100]
// 키에 해당하는 값 변경
anyDictionary["someKey"] = "dictionary"
print(anyDictionary) ["someKey": "dictionary", "anotherKey": 100]
// 키에 해당하는 값 제거
anyDictionary.removeValue(forKey: "anotherKey")
anyDictionary["someKey"] = nil
print(anyDictionary)
// 3. 불변 Dictionary: let을 사용하여 Dictionary 선언
let emptyDictionary: [String: String] = [:]
let initalizedDictionary: [String: String] = ["name": "GGDol", "gender": "male"]
// 불변 Dictionary이므로 값 변경 불가
//emptyDictionary["key"] = "value"
// 키에 해당하는 값을 다른 변수나 상수에 할당하고자 할 때는 옵셔널과 타입 캐스팅 파트에서 다룹니다
// "name"이라는 키에 해당하는 값이 없을 수 있으므로 String 타입의 값이 나올 것이라는 보장이 없습니다.
// 컴파일 오류가 발생합니다
// let someValue: String = initalizedDictionary["name"]
Set
- 중복되지 않는 멤버가 순서없이 존재하는 컬렉션
- Array, Dictionary와 다르게 축약형이 존재하지 않음
// 1. Set 생성 및 선언
var integerSet: Set<Int> = Set<Int>()
// insert : 새로운 멤버 입력
// 동일한 값은 여러번 insert해도 한번만 저장됩니다.
integerSet.insert(1)
integerSet.insert(99)
integerSet.insert(99)
integerSet.insert(99)
integerSet.insert(100)
print(intigerSet) // {100, 99, 1}
// contains: 멤버 포함 여부 확인
print(integerSet.contatins(1)) // true
print(integerSet.contains(2)) // false
// remove: 멤버 삭제
integerSet.remove(99) // {100, 1}
integerSet.removeFirst() // {1}
// count: 멤버 개수
integerSet.count // 1
// 2. 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(intersection) // [5, 3, 4]
// 차집합
let subtracting: Set<Int> = setA.subtracting(setB)
print(subtracting) // [2, 1]
2. 생각해보기
▶ 다음과 같은 경우에는 각각 어떤 컬렉션 타입을, 상수/변수 선언 중 어떤 것을 사용하면 유용할지 생각해 봅시다.
- 영어 알파벳 소문자를 모아두는 컬렉션
- 책의 제목과 저자 정리를 위한 컬렉션
- 수강생 명부 작성을 위한 컬렉션
공공돌의 생각 💬
import UIKit
let alphabet: Set<Character> = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
let sortedAlphabet = alphabet.sorted()
print(sortedAlphabet)
var bookList: Dictionary<String, String> = [:]
bookList["꼼꼼한 재은씨의 Switf 기본편"] = "이재은"
bookList["스위프트 프로그래밍: Swift 5"] = "야곰"
print(bookList)
var studentName = [String]()
studentName.append("공")
studentName.append("공")
studentName.append("돌")
print(studentName)
알파벳 소문자를 모아두는 컬렉션의 경우 중복 값이 존재하지 않게 해주는 Set이 적절하다고 생각합니다.
물론 순서의 영향이 있을 수 있겠지만 sorted를 활용하여 정렬을 해주면 된다고 판단했습니다.
책 제목과 저자 정리를 위한 컬렉션의 경우 책과 저자가 쌍을 이루고 있기 때문에 키와 값으로 이루어진 Dictionary가 적절하다고 생각합니다.
수강생 명부 작성의 경우 동명이인이 있을 수 있기에 Set은 부합하다고 판단하여 Array가 적절하다고 생각합니다.
공부하는 공돌이, 공공돌입니다🐻
@sheep1sik
출처 : 야곰 iOS 프로그래밍을 위한 스위프트 기초
반응형
'iOS > Swift 기초' 카테고리의 다른 글
[ Swift 기초 ] 함수 고급 (2) | 2024.01.04 |
---|---|
[ Swift 기초 ] 함수 기본 (2) | 2024.01.03 |
[ Swift 기초 ] Any, AnyObject, nil (2) | 2024.01.02 |
[ Swift 기초 ] 기본 데이터 타입 (2) | 2024.01.02 |
[ Swift 기초 ] 상수와 변수 (1) | 2024.01.02 |