본문 바로가기
 IOS,Swift/Swift⌘

Swift - Collection Type1 Array

by 시에라177 2022. 6. 28.

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 unordered collections of key-value associations.

-> 스위프트는 세가지의 주요한 컬렉션 타입을 제공. 어레이, 집합, 딕셔너리가 바로 그것. 어레이는 정렬된 값들의 컬렉션이고, 집합은 순서가 없고 중복이 없는 값들의 컬렉션, 딕셔너리는 순서가 없는 키-벨류 쌍의 모임.

https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html

 

#0 Mutability of Collections

If you create an array, a set, or a dictionary, and assign it to a variable, the collection that’s 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 can’t be changed.

-> 컬렉션 타입은 요소를 삭제하든 추가하든 변경하든 변하기 쉽다. 그러나 컬렉션 타입도 상수형으로 선언하면 불변하는 컬렉션 타입이 된다.

 

 

#1 Arrays

-shorthand Systax

The type of a Swift array is written in full as Array<Element>, where Element is the type of values the array is allowed to store. You can also write the type of an array in shorthand form as [Element]. Although the two forms are functionally identical, the shorthand form is preferred and is used throughout this guide when referring to the type of an array.

-> Array<Element type>이 완전한 어레이의 타입 선언이지만, [Element type]과 같은 간단한 구문도 허용되고, 이 문법이 더 자주 사용됨.

 

-Empty Array

var emptyInts: Array<Int> = Array<Int>()
var emptyInts: Array<Int> = [Int]()
var emptyInts: Array<Int> = []
var emptyInts: [Int] = Array[Int]()
var emptyInts: [Int] = [Int]()
var emptyInts: [Int] = []
var emptyInts = [Int]()

 

-Creating an Array with a Default Value

"Swift’s Array type also provides an initializer for creating an array of a certain size with all of its values set to the same default value."

-> 스위프트는 같은 디폴트 값으로 이루어진 배열을 만들 수 있도록 해줌.

 

-Creating an Array by Adding Two Arrays Together

You can create a new array by adding together two existing arrays with compatible types with the addition operator (+). The new array’s type is inferred from the type of the two arrays you add together

-> 두 개의 어레이를 +기호로 합함으로써 새로운 어레이를 만들 수 있습니다. 이때 새로운 어레이의 타입은 추론됨.

 

 

-Accessing and Modifying an Array

-> append 와 insert의 차이. insert는 특정한 인덱스에 값을 넣을 때 사용

 

-Iterating Over an Array

-> 어레이 반복하기

' IOS,Swift > Swift⌘' 카테고리의 다른 글

Swift - Optional  (0) 2022.07.01
Swift - Enumeration  (0) 2022.06.30
Swift - Declaring Constants(let) & Variables(var)  (0) 2022.06.26
Swift - String & Characters  (0) 2022.06.25
Swift - Function 1  (0) 2022.06.25