Enumeration : 열거, 목록

# 0 Enumeration
⌘An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.
-> 열거형은 연관된 값들을 공통된 타입으로 묶는다. 그리고 코드의 타입안정성을 가능케 해준다.
-이미 정해놓은 값만을 입력을 통해 받길 원할 때 사용한다.
-스위프트 열거형은 case가 그 자체로 value가 될 수 있음
#1 Enumeration Syntax
⌘You introduce enumerations with the enum keyword and place their entire definition within a pair of braces
->열거형은 "enum"이라는 키워드로 시작하며, 중괄호로 정의 묶는다.
⌘The values defined in an enumeration (such as north, south, east, and west) are its enumeration cases. You use the case keyword to introduce new enumeration cases
-> 열거형 안에 정의된 값들은 열거형의 case들이다. 새로운 case를 만들 때 case키워드를 사용한다.
-> 굳이 case를 여러번 쓸 필요없이, case한번에 값들을 콤마로 구분해 정의해줄 수도 있다.

⌘Each enumeration definition defines a new type. Like other types in Swift start with a capital letter. Give enumeration types singular rather than plural names, so that they read as self-evident
->각각의 열거형의 정의는 새로운 타입을 정의한다. 스위프트의 다른 타입들과 같이 열거형은 대문자로 시작한다. 열거형의 타입에는 복수형의 이름을 부여하여 명확하게 읽히도록 하는 것이 좋다.
-> 변수나 상수를 해당 열거형 타입으로 초기화하여 타입을 명확히 하였을 때, 다음 번에는 dot syntax와 케이스만을 이용하여 짧게 표현할 수 있다.

# 1-1 When use in Function
-함수 내부에선 매개변수명을 사용한다.
-함수 외부에서 인자로 넘길 때는 "열거형.case"로 넘긴다.

-만약 원시값을 사용한다면 함수 내부에서 "매개변수.rawValue"로 명시한다.

#2 Matching Enumeration Values with a Switch Statement
⌘You can match individual enumeration values with a switchstatement
-> 각각의 열거형 값들을 switch 조건문에 사용할 수 있다.


⌘As described in Control Flow, a switch statement must be exhaustive when considering an enumeration’s cases.Requiring exhaustiveness ensures that enumeration cases aren’t accidentally omitted.When it isn’t appropriate to provide a case for every enumeration case, you can provide a default case to cover any cases that aren’t addressed explicitly
-> 스위치 구문에 열거형의 케이스들을 쓴다면 매우 철저해야함. 단 하나의 케이스라도 생략된다면 그 스위치 구문을 컴파일 되지 않는다.
그래도 만약 모든 열거형 케이스를 제공하지 않는다면, default를 사용함으로써 사용되지 않는 열거형 케이스들을 커버할 수 있다.
#3 Iterating over Enumeration Cases
⌘For some enumerations, it’s useful to have a collection of all of that enumeration’s cases. You enable this by writing : CaseIterable after the enumeration’s name. Swift exposes a collection of all the cases as an allCases property of the enumeration type. Here’s an example
-> 특정한 열거형은 해당 열거형의 케이스들의 컬렉션으로 사용할 수 있다. 이 것은 열거형의 이름뒤에 : CaseIterable을 적어줌으로써 가능하다. 스위프트는 해당 열거형의 모든 케이스들(allCases)로 컬렉션을 만든다.


#4 Raw Values
⌘ As an alternative to associated values, enumeration cases can come prepopulated with default values (called raw values), which are all of the same type. Each raw value must be unique within its enumeration declaration. Raw values can be strings, characters, or any of the integer or floating-point number types.
-> 열거형 케이스들은 디폴트 값으로 채워넣을 수도 있습니다. 그리고 이 값들은 원시값이라 부릅니다. 한 열거형의 원시값들은 모두 같은 타입이어야 합니다. 원시값은 String, Character, Integer, Float 을 사용할 수 있고 원시값은 고유해야 합니다.

# 5 implicitly Assigned Raw Values
⌘When you’re working with enumerations that store integer or string raw values, you don’t have to explicitly assign a raw value for each case. When you don’t, Swift automatically assigns the values for you.
-> 열거형의 원시값에 정수나 문자열을 저장할 때, 반드시 명시적으로 원시값을 할당해줄 필요는 없다. 스위프트가 자동으로 해준다.
원시값 타입이 정수인 경우 케이스 마다 1씩 더해진 원시값을 가진다. 원시값을 사용하고자 할때는 [열거형.케이스.rawValue].
문자열인 경우에는 원시값으로 케이스명이 사용된다.

#6 Initializing from a Raw Value
⌘If you define an enumeration with a raw-value type, the enumeration automatically receives an initializer that takes a value of the raw value’s type (as a parameter called rawValue) and returns either an enumeration case or nil. You can use this initializer to try to create a new instance of the enumeration.
-> 만약 열거형을 원시값 타입과 정의했다면, 열거형은 이니셜라이저를 받아 자동적으로 해당 원시값의 case를 찾아 변수나 상수의 타입을 만들어 줄 수 있다. 즉 원시값을 통해 객체를 초기화 할 수 있다. 해당 원시값이 없다면 nil이 할당된다.

#7 Recursive Enumerations
- 재귀 열거자
' IOS,Swift > Swift⌘' 카테고리의 다른 글
| Swift - Optional Chaining (0) | 2022.07.02 |
|---|---|
| Swift - Optional (0) | 2022.07.01 |
| Swift - Collection Type1 Array (0) | 2022.06.28 |
| Swift - Declaring Constants(let) & Variables(var) (0) | 2022.06.26 |
| Swift - String & Characters (0) | 2022.06.25 |