๋ฐ์ํ
๐ ๋ชฉ๋ก
- ์ด๊ฑฐํ ์ด๋?
- ๊ธฐ๋ณธ ๋ฌธ๋ฒ
- ๊ธฐ๋ณธ๊ฐ ๋ณ๊ฒฝ
- ํ ๋น ๋ฌธ๋ฒ
- ์ฐ๊ด ๊ฐ ๋ช ์
๐ ์ด๊ฑฐํ ์ด๋?
์ฐ๊ด์ฑ์ด ์๋ ๊ฐ์ ๋ชจ์ ๋์ ๊ฒ์ ๋งํ๋ค.
๐ ๊ธฐ๋ณธ ๋ฌธ๋ฒ
enum CompassPoint: String {
case north
case south
case east
case west
// case north, south, east, west -> ์ด๋ ๊ฒ ํ์ค๋ก๋ ํ๊ธฐ ๊ฐ๋ฅํฉ๋๋ค.
}
var direction = CompassPoint.east // east
direction = .west // west -> ์ฌ๊ธฐ์ ํ์
์ถ๋ก ์ด ์ด๋ฏธ ๊ฐ๋ฅํด์ก๊ธฐ ๋๋ฌธ์ .์ผ๋ก๋ง ์ ๊ทผ์ด ๊ฐ๋ฅ
๐ ๊ธฐ๋ณธ ๊ฐ ๋ณ๊ฒฝ
๊ธฐ๋ณธ๊ฐ์ ์ด๋ ๊ฒ ๋ณ๊ฒฝ์ ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
enum CompassPoint: String {
case north = "๋ถ"
case south = "๋จ"
case east = "๋"
case west = "์"
case none = "๋
ผ"
}
var direction = CompassPoint.east // east
direction = .west // west
switch direction {
case .north:
print(direction.rawValue)
case .west:
print(direction.rawValue)
case .east:
print(direction.rawValue)
case .south:
print(direction.rawValue)
default:
print(direction.rawValue)
}
// '์' ์ถ๋ ฅ
๐ ํ ๋น ๋ฌธ๋ฒ
enum CompassPoint: String {
case north = "๋ถ"
case south = "๋จ"
case east = "๋"
case west = "์"
case none = "๋
ผ"
// case north, south, east, west
}
let direction2 = CompassPoint(rawValue: "๋จ") // south
๐ ์ฐ๊ด ๊ฐ ๋ช ์
enum PhoneError {
case unknown
case batteryLow(String)
}
let error = PhoneError.batteryLow("๋ฐฐํฐ๋ฆฌ๊ฐ ๊ณง ๋ฐฉ์ ๋ฉ๋๋ค.") // "๋ฐฐํฐ๋ฆฌ๊ฐ ๊ณง ๋ฐฉ์ ๋ฉ๋๋ค."
// ์ฐ๊ด๊ฐ ์ถ์ถ
switch error {
case .batteryLow(let message):
print(message)
case .unknown:
print("์์ ์๋ ์๋ฌ")
}
// ๋ฐฐํฐ๋ฆฌ๊ฐ ๊ณง ๋ฐฉ์ ๋ฉ๋๋ค.
๋ฐ์ํ
'Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Swift try-catch (0) | 2021.11.28 |
---|---|
Swift ์ต์ ๋ ์ฒด์ด๋ (0) | 2021.11.28 |
Swift ์ต์คํ ์ (0) | 2021.11.28 |
Swift ํ๋กํ ์ฝ (0) | 2021.11.28 |
Swift asset ์ guard (0) | 2021.11.28 |