Skip to content

Commit

Permalink
docs: enumerations 8
Browse files Browse the repository at this point in the history
์›์‹œ๊ฐ’ (Raw Values)
  • Loading branch information
mazdah committed Mar 2, 2022
1 parent cfcbf9f commit a3755ae
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions _posts/Swift/2022-03-02-Enumerations_4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
layout: article
title: Swift - Enumeration 4
excerpt: Swift์˜ Enumeration ์ •๋ฆฌ 4 - Raw Values
mathjax: true
tags:
- [Swift, Enumeration, switch, Raw Values]
categories: Swift
comments: true
header:
theme: dark
background: 'linear-gradient(135deg, rgb(34, 139, 87), rgb(139, 34, 139))'
article_header:
type: overlay
theme: dark
background_color: '#203028'
background_image:
gradient: 'linear-gradient(135deg, rgba(34, 139, 87 , .4), rgba(139, 34, 139, .4))'
src: /swift.jpg
---

# Enumeration #4
- - - -
### ์›์‹œ๊ฐ’ (Raw Values)

- ์—ฐ๊ด€๊ฐ’ ๋Œ€์‹  enumeration ์ผ€์ด์Šค ๋ชจ๋‘์— ๊ฐ™์€ ํƒ€์ž…์ด ๊ธฐ๋ณธ๊ฐ’์„ ๋ฏธ๋ฆฌ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

```swift
enum ASCIIControlCharacter: Character {
case tab = "\t"
case lineFeed = "\n"
case carriageReturn = "\r"
}
```

- ASCIIControlCharacter๋Š” Character ํƒ€์ž…์œผ๋กœ ์›์‹œ๊ฐ’์ด ์„ค์ •๋˜์–ด ์žˆ๋‹ค.
- ์›์‹œ๊ฐ’์€ Character ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฌธ์ž์—ด, ์ •์ˆ˜, ๋ถ€๋™์†Œ์ˆซ์  ์ˆซ์ž ํƒ€์ž…๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
- ๊ฐ ์ผ€์ด์Šค์˜ ์›์‹œ ๊ฐ’์€ enumeration ์•ˆ์—์„œ ์œ ์ผํ•œ ๊ฐ’์ด์–ด์•ผ ํ•œ๋‹ค.

> ์›์‹œ๊ฐ’์€ enumeration ์ฒ˜์Œ ์ƒ์„ฑ์‹œ ์„ค์ •ํ•˜๋Š” ๊ฐ’์œผ๋กœ ํ•ญ์ƒ ๋™์ผํ•œ ๊ฐ’์œผ๋กœ ์œ ์ง€๋˜๋‚˜ ์—ฐ๊ด€๊ฐ’์€ ๊ฐ ์ผ€์ด์Šค์— ํƒ€์ž…๋งŒ์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ƒ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๊ฐ’์ด ์„ค์ •๋˜๋ฉฐ ๊ทธ ๊ฐ’์€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค.
#### ์•”์‹œ์ ์œผ๋กœ ํ• ๋‹น๋œ ์›์‹œ๊ฐ’ (Implicitly Assigned Raw Values)

- ์›์‹œ๊ฐ’์˜ ํƒ€์ž…์ด ์ •์ˆ˜ ๋˜๋Š” ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ enumeration์˜ ๊ฐ ์ผ€์ด์Šค์— ๋ช…์‹œ์ ์œผ๋กœ ์›์‹œ๊ฐ’์„ ํ• ๋‹นํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค(Swift๊ฐ€ ์ž๋™์œผ๋กœ ํ• ๋‹น).
- ์˜ˆ๋ฅผ ๋“ค์–ด ์›์‹œ๊ฐ’์˜ ํƒ€์ž…์ด ์ •์ˆ˜ํ˜•์ธ ๊ฒฝ์šฐ ๊ฐ ์ผ€์ด์Šค๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ด์ „ ์ผ€์ด์Šค๋ณด๋‹ค 1์ด ์ฆ๊ฐ€๋œ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ, ๋งŒ์ผ ์ฒซ ๋ฒˆ์งธ ์ผ€์ด์Šค์— ๊ฐ’์ด ์ง€์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ 0์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.

```swift
enum Planet: Int {
case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}
```

- ์œ„์˜ ์˜ˆ์—์„œ Planet.mercury๋Š” ๋ช…์‹œ์ ์œผ๋กœ 1์„ ์„ค์ •ํ•ด์ฃผ์—ˆ๊ณ , ๋”ฐ๋ผ์„œ Planet.venus๋Š” ์•”์‹œ์ ์œผ๋กœ 1์ด ์ฆ๊ฐ€ํ•œ 2์˜ ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค.

```swift
enum CompassPoint: String {
case north, south, east, west
}
```

- ์œ„ ์˜ˆ์ œ์˜ ๊ฒฝ์šฐ ์›์‹œ๊ฐ’์˜ ํƒ€์ž…์ด ๋ฌธ์ž์—ด์ด๊ณ  ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •๋œ ๊ฐ’์ด ์—†๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ๊ฐ ์ผ€์ด์Šค๋Š” ์•”์‹œ์ ์œผ๋กœ ์ž์‹ ์˜ ์ด๋ฆ„์„ ์›์‹œ๊ฐ’์œผ๋กœ ๊ฐ–๋Š”๋‹ค.
- CompassPoint.south์˜ ์›์‹œ๊ฐ’์€ โ€œsouthโ€์ด๋‹ค.
- ์›์‹œ๊ฐ’์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” rawValue๋ผ๋Š” ์†์„ฑ์„ ์‚ฌ์šฉํ•œ๋‹ค.

```swift
let earthsOrder = Planet.earth.rawValue
// earthsOrder๋Š” 3์˜ ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค.

let southDirection = CompassPoint.west.rawValue
// southDirection๋Š” "west" ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค.
```


#### ์›์‹œ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™” (Initializing from a Raw Value)

- rawValue๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ enumeration์„ ์ดˆ๊ธฐํ™” ํ•˜๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ์ผ€์ด์Šค ๋˜๋Š” ํ•ด๋‹น ์ผ€์ด์Šค๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ nil์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

```swift
let possiblePlanet = Planet(rawValue: 7)
// possiblePlanet์€ Planet? ํƒ€์ž…์ด๊ณ  Planet.uranus์˜ ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค.
```

> ์›์‹œ๊ฐ’ ์ดˆ๊ธฐํ™”๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์— ํ•ด๋‹นํ•˜๋Š” ์ผ€์ด์Šค๊ฐ€ ์—†์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐํ™”์— ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค.
- ์œ„ ์˜ˆ์ œ์—์„œ ๋งŒ์ผ 11 ๊ฐ’์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ดˆ๊ธฐํ™” ์‹œ ๋ฐ˜ํ™˜๋˜๋Š” ์˜ต์…”๋„ Planet์˜ ๊ฐ’์€ nil์ด๋‹ค.

```swift
let positionToFind = 11
if let somePlanet = Planet(rawValue: positionToFind) {
switch somePlanet {
case .earth:
print("Mostly harmless")
default:
print("Not a safe place for humans")
}
} else {
print("There isn't a planet at position \(positionToFind)")
}

// "There isn't a planet at position 11"์ด ์ถœ๋ ฅ๋œ๋‹ค.
```

0 comments on commit a3755ae

Please sign in to comment.