본문 바로가기

Programming/Swift

Collection Types in Swift

Swift provides three primary collection types, known as 1. arrays, 2. sets, and 3. dictionaries, for storing collections of values.

 

1. Arrays are ordered collections of values.

2. Sets are unordered collections of unique values.

3. Dictionaries are unordered collections of key-value associations.

 

Array vs. Dictionary

Array > 순서 기반으로 찾음
Dictionary> 의미있는 값을 기반으로 접근하게 됨

이 학생의 점수는 이거다
순서랑 상관없이
이름과 점수의 짝으로 관리할 땐 Dictionary로 관리하면 편함

Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store. This means that you cannot insert a value of the wrong type into a collection by mistake. It also means you can be confident about the type of values you will retrieve from a collection.


NOTE

Swift’s array, set, and dictionary types are implemented as generic collections. For more about generic types and collections, see Generics.

Mutability of Collections

If you create an array, a set, or a dictionary, and assign it to a variable, the collection that is created will be mutable. This means that you can change (or mutate) the collection after it’s created by adding, removing, or changing items in the collection. If you assign an array, a set, or a dictionary to a constant, that collection is immutable, and its size and contents cannot be changed.

 

NOTE

It is good practice to create immutable collections in all cases where the collection does not need to change. Doing so makes it easier for you to reason about your code and enables the Swift compiler to optimize the performance of the collections you create.

Arrays

An array stores values of the same type in an ordered list. The same value can appear in an array multiple times at different positions.

 

NOTE

Swift’s Array type is bridged to Foundation’s NSArray class.

For more information about using Array with Foundation and Cocoa, see Bridging Between Array and NSArray.

 

 

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

 

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

Swift - 조건문  (0) 2020.12.14
Value and Reference Types  (0) 2020.12.10
Can an array contain different data types?  (0) 2020.11.27
When to use guard let rather than if let  (0) 2020.11.27
Nil coalescing  (0) 2020.11.25