Swift Nested Types
Nested Types create hierarchies and encapsulate Classes or Structures or Enumerations.
1. Nested Types π©βπ»
Enumerations λ μ£Όλ‘ νΉμ Classes λλ Structures μ κΈ°λ₯μ μ§μνκΈ° μν΄ μ¬μ©λλ€. μ μ¬νκ² λ 볡μ‘ν νμ
μ
context μμ μ¬μ©νκΈ° μν΄ μμνκ² Utility Classes or Structures
λ₯Ό μ μνλ κ²μ΄ νΈλ¦¬ν μλ μλ€.
μ΄λ₯Ό μν΄ Swift μ Classes, Structures, Enumerations λ λͺ¨λ Nested Types
λ₯Ό μ§μνλ€. μ΄λ₯Ό ν΅ν΄ scope
λ₯Ό
μ νν μ μλ€. Nested Types λ μ§μνλ νμ
μ λ΄λΆ μ€κ΄νΈ λ΄μ μμ±ν΄μΌνλ©°, Nested Types λ νμν λ§νΌ μ€μ²©μ΄ κ°λ₯νλ€.
Nested Functions μμ μ΄λ―Έ νμ μ μ€μ²©μ μμ±ν΄λ³Έ μ μλ€. Swift λ First-Class Citizens λ₯Ό μ§μνλ μΈμ΄λ‘ μ΄ κΈμμ μ€λͺ νλ €λ Classes, Structures, Enumerations Types μ μ€μ²©μ Function Types μ μ€μ²©κ³Ό ν¬κ² λ€λ₯΄μ§ μλ€.
2. Nested Types in Action π©βπ»
λΈλμ κ²μμ μν΄ Structure λ₯Ό μ μν΄λ³΄μ. μΉ΄λμ λͺ¨μμ λ°λΌ μΈνΈλ₯Ό λλλ Suit
Enumeration, μΉ΄λμ μ«μμ λ°λΌ λ±κΈμ λλλ
Rank
Enumeration λ₯Ό Nested Types λ‘ κ°λλ‘ ν μ μλ€.
κ·Έλ¦¬κ³ Rank
λ λ€μ βλ°λμ μ μ₯ν κ° νλβμ βOptional λ‘ μ μ₯ν λ€λ₯Έ κ° νλβλ₯Ό ꡬ쑰ν νκΈ° μν΄ Values
Structure λ₯Ό
Nested Types λ‘ κ°λλ€. κ·Έλ¦¬κ³ Rank
κ° νμ μ΅μ value κ°μ μ»λλ‘ νκΈ° μν΄ Values
Type μ Computed Property λ₯Ό
values
λ³μλ₯Ό κ°λλ€.
μ΄μ μ΄ λ Nested Types λ₯Ό κ°μ§κ³ BlackjackCard Structure λ₯Ό μ μνλ€. BlackjackCard μ rank μ suit λ₯Ό properties λ‘ κ°κ³ , μ΄κ²μ μ€λͺ νκΈ° μν description μ computed property λ‘ κ°λλ€.
struct BlackjackCard {
// nested Suit enumeration
enum Suit: Character {
case spades = "β ", hearts = "β‘", diamonds = "β’", clubs = "β£"
}
// nested Rank enumeration
enum Rank: Int {
case two = 2, three, four, five, six, seven, eight, nine, ten
case jack, queen, king, ace
struct Values {
let first: Int, second: Int?
}
var values: Values {
switch self {
case .ace:
return Values(first: 1, second: 11)
case .jack, .queen, .king:
return Values(first: 10, second: nil)
default:
return Values(first: self.rawValue, second: nil)
}
}
}
// BlackjackCard properties and methods
let rank: Rank, suit: Suit
var description: String {
var output = "suit is \(suit.rawValue),"
output += " value is \(rank.values.first)"
if let second = rank.values.second {
output += " or \(second)"
}
return output
}
}
let aceOfSpades = BlackjackCard(rank: .ace, suit: .spades)
let kingOfHearts = BlackjackCard(rank: .king, suit: .hearts)
let sixOfDiamonds = BlackjackCard(rank: .six, suit: .diamonds)
print("The ace of spades : \(aceOfSpades.description)")
print("The king of hearts : \(kingOfHearts.description)")
print("The six of diamonds : \(sixOfDiamonds.description)")
The ace of spades : suit is β , value is 1 or 11
The king of hearts : suit is β‘, value is 10
The six of diamonds : suit is β’, value is 6
3. Referring to Nested Types π©βπ»
Nested Types
λ κΈ°λ³Έμ μΌλ‘ μ΄κ²μ΄ μ μλ context μ λ΄λΆλ‘ λ²μκ° μ ν
λλ€. μ΄λ κ² μΊ‘μν ν¨μΌλ‘μ¨ μ μμμ μ κ·Όν νμκ° μλ
Types λ₯Ό μ¨κ²¨ μ½λλ₯Ό λμ± μμ νκ³ κ°λ
μ± λκ² λ§λ€ μ μλ€. νμ§λ§ Nested Types λ Closures μ Capturing Values μ λ€λ₯΄κ²
μμ ν 격리λλ κ²μ μλλ€.
λ§μ½ 2~3κ°μ νμ
μμλ§ μ¬μ©ν μ΄λ€ Nested Types
κ° μλ€κ³ ν΄λ³΄μ. μ΄λ₯Ό μ μμΌλ‘ λ§λ€λ©΄ 2~3κ°μ νμ
μμ 곡μ κ° κ°λ₯νλ€. νμ§λ§ μ΄λ°
νμ
μ΄ λ§μμ§λ©΄ μ μ scope μ€μΌ
μ΄ μΌμ΄λ λΆνμνκ² λ³΅μ‘ν΄μ§ κ°λ₯μ±μ΄ λλ€. μ΄λ₯Ό νΌνκΈ° μν΄ 2~3κ°μ νμ
λ§λ€ λμΌν Nested Types λ₯Ό
λ§λ€λ©΄ μ μ scope μ€μΌμ λμ§ μκ² μ§λ§, μ½λμ μ€λ³΅μ΄ λ°μνκ³ μ μ§λ³΄μ μ μ½λμ λ³κ²½ μ¬νμ λμ³ Human Errors λ₯Ό λ§λλ μμΈμ΄ λ μ μλ€.
μ΄λ° κ²½μ° κ°μ₯ μ°κ΄μ±μ΄ λμ κ³³μ νλμ Nested Types λ₯Ό μ μνκ³ , μΈλΆ νμ
μμ μ κ·Όν κ²½μ° ν΄λΉ Nested Types κ° μ μλ κ°μ₯ μΈλΆ
Types μ μ κ·Όν΄ Hierarchy ꡬ쑰λ₯Ό νκ³ λ΄λ €κ° λͺ
μμ μΌλ‘ μ κ·Ό
ν μ μλ€. μ΄λ κ² λͺ
μμ μΈ μ κ·Όμ νμ©ν¨μΌλ‘μ¨ Nested Types λ
μ μ scope μ μ€μΌμ μλ°©νλ©° νμμ λ°λΌ λͺ
μμ μ κ·Όμ ν΅ν μ½λμ μ¬μ¬μ©μ±μ λμμ ν보
ν μ μλ€.
let heartsSymbol = BlackjackCard.Suit.hearts.rawValue
print(heartsSymbol) // β‘
Reference
- βNested Types.β The Swift Programming Language Swift 5.7. accessed Jan. 16, 2023, Swift Docs Chapter 19 - Nested Types.