티스토리 뷰

Swift는 세 가지 '컬렉션 타입'을 갖고 있습니다.

 

각각의 특징에 따라 필요한 기능에 사용하면 됩니다!

 

1. Array

- 멤버가 순서(Index)를 가지는 리스트 형태의 컬렉션 타입입니다.

- Index를 이용해 random access가 가능합니다.

- let 을 사용해 선언한 불변 Array 는 수정이 불가능합니다.

// Int 타입의 Array 선언 및 생성
var intArr: Array<Int> = Array<Int>()

//위와 동일한 표현
var intArr: Array<Int> = [Int]()
var intArr: Array<Int> = []
var intArr: [Int] = Array<Int>()
var intArr: [Int] = [Int]()
var intArr: [Int] = []
var intArr = [Int]()

// Array 요소 추가
intArr.append(1)
intArr.append(100)

// Array 타입과 다른 값 추가 불가
intArr.append(1.3) // error 발생

// Array 멤버 수 확인
print(intArr.count) // 2 출력

print(intArr) // [1, 100] 출력

//Array 멤버 포함 여부 확인
print(intArr.contains(100)) // true 출력
print(intArr.contains(2)) // false 출력

//Array 멤버 교체
intArr[0] = 2

// Array 멤버 삭제
intArr.remove(at: 0) // 0번 index 요소 삭제
intArr.removeLast() // 마지막 요소 삭제
intArr.removeAll() // 모든 요소 삭제

let immuArr = [1,2,3] // 불변 Array 선언
// let으로 선언한 Array는 멤버의 추가 삭제 불가
immuArr.append(1) // error 발생

 

2. Dictionary

- 'Key'와 'Value' 쌍으로 이루어진 컬렉션 타입입니다.

- hash table 과 유사한 기능을 제공합니다.

// Dictionary 선언 및 생성
// Key : String, Value: Any 타입
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]()

// Dictionary 활용
anyDictionary["someKey"] = "value" // "someKey" Key 에 "value" 값 할당
anyDictionary["anotherKey"] = 100 // value 가 Any 타입이기에 여러 타입 할당가능

print(anyDictionary) // ["someKey": "value", "anotherKey": 100] 출력

// key에 해당하는 값 변경
anyDictionary["someKey"] = "hello"
print(anyDictionary) // ["someKey": "hello", "anotherKey": 100] 출력

// key에 해당하는 값 제거
anyDictionary.removeValue(forKey: "someKey")
anyDictionary["anotherKey"] = nil

// 불변 Dictionary 선언
let immuDictionary: [String:Int] = [:]
// 불변 Dictionary 수정 불가
immuDictionary["key"] = "value" //  error 발생

 

3. Set

- Key 값 만을 멤버로 저장하는 컬렉션입니다.

- 중복되지 않는 멤버가 순서없이 존재합니다. 멤버의 유일성이 보장되기 때문에 집합 연산에 활용하면 유용합니다.

// Set 생성 및 선언
var integerSet: Set<Int> = Set<Int>()

// insert : 새로운 멤버 입력
integerSet.insert(1)
integerSet.insert(99)
integerSet.insert(99)
integerSet.insert(99)
integerSet.insert(100)

print(integerSet) // {100, 99, 1}

// contains: 멤버 포함 여부 확인
print(integerSet.contains(1)) // true
print(integerSet.contains(2)) // false

// remove: 멤버 삭제
integerSet.remove(99) // {100, 1}
integerSet.removeFirst() // {1}

// count: 멤버 개수
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(intersection) // [5, 3, 4]

// 차집합
let subtracting: Set<Int> = setA.subtracting(setB)
print(subtracting) // [2, 1]

 

* [부스트코스] iOS 프로그래밍을 위한 스위프트 기초 학습 후 정리한 내용입니다.

'iOS > Swift' 카테고리의 다른 글

Swift 문법 (6) - 반복문  (0) 2020.12.06
Swift 문법 (5) - 조건문  (0) 2020.12.03
Swift 문법 (4) - 함수  (0) 2020.12.02
Swift 문법 (2) - Any, AnyObject, nil  (0) 2020.11.29
Swift 문법 (1) - 상수/변수, 기본 데이터타입  (0) 2020.11.29
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/10   »
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
글 보관함