티스토리 뷰
출처
중첩된 타입 (Nested Types)
중첩 타입에서의 동작
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 theAceOfSpades = BlackjackCard(rank: .ace, suit: .spades)
print("theAceOfSpades: \(theAceOfSpades.description)")
// Prints "theAceOfSpades: suit is ♠, value is 1 or 11"
let heartsSymbol = BlackjackCard.Suit.hearts.rawValue
// heartsSymbol is "♡"
'iOS 개발 > Swift' 카테고리의 다른 글
[Swift] Singleton Pattern 구현하기 (0) | 2017.06.06 |
---|---|
[Swift 3] 확장 (Extensions) (0) | 2017.04.22 |
[Swift 3] 타입 변환 (Type Casting) (0) | 2017.04.20 |
[Swift 3] 오류 처리 (Error Handling) (0) | 2017.04.19 |
[Swift 3] 옵셔널 체이닝 (Optional Chaining) (0) | 2017.04.18 |
- Total
- Today
- Yesterday
- applicationWillResignActive
- workerThread
- UIView
- Swfit
- Arc
- AWS
- dictionary
- 읽기 좋은 코드가 좋은 코드다
- docker
- NSManagedObjectModel
- EffectiveObjectiveC
- set
- NSManagedObjectContext
- optional
- Block
- NSManagedObject
- thread
- CGImage
- Swift 3
- Swift 3.0
- 꺼내먹어요
- HTTP
- coredata
- CIImage
- Swift3
- delegate
- RunLoop
- string
- ios
- Swift
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |