티스토리 뷰
실습 내용입니다.
Enum
Objc에서 Int 값으로 사용했으나 swift에서는 다른 타입들도 사용이 가능합니다.
String 타입으로 선언한 enum입니다.
enum Suit: String {
case spades = "♠️"
case hearts = "❤️"
case dimonds = "♣️"
case clubs = "♦️"
}
연관 데이터를 전달할 수 있습니다.
1~13까지의 카드를 표현하기 위해서는 예전엔 아래와 같은 방식으로 작성했을것 입니다.
enum Rank {
case ace
case two
case three
...
case jack
case queen
case king
}
연관된 데이터를 사용하여 작성시 아래와 같은 코드로 작성이 가능합니다.
enum Rank {
case ace
case face(String) // J,Q,K
case numeric(Int) // 2~10
}
enum내 함수 추가
클래스나 구조체 타입처럼 함수를 가질수 있습니다. 자신의 값을 사용하기 위해선 self를 사용합니다.
enum Rank {
case ace
case face(String)
case numeric(Int)
var order: Int {
switch self {
case .ace: return 1
case .numeric(let pips): return pips
case .face(let kind) where kind == "J": return 11
case .face(let kind) where kind == "Q": return 12
case .face(let kind) where kind == "K": return 13
default: return 0
}
}
}
객체 내용 Print 하기
for _ in 1...10 {
if let card = deck.draw() {
print("\(card)")
}
}
viewDidLoad에서 랜덤으로 생성한 카드를 print시 래와 같은 로그가 출력됩니다.
PlayingCard(suit: PlayingCard.PlayingCard.Suit.spades, rank: PlayingCard.PlayingCard.Rank.face("J")) PlayingCard(suit: PlayingCard.PlayingCard.Suit.dimonds, rank: PlayingCard.PlayingCard.Rank.face("K")) PlayingCard(suit: PlayingCard.PlayingCard.Suit.hearts, rank: PlayingCard.PlayingCard.Rank.face("K")) PlayingCard(suit: PlayingCard.PlayingCard.Suit.spades, rank: PlayingCard.PlayingCard.Rank.face("Q")) PlayingCard(suit: PlayingCard.PlayingCard.Suit.clubs, rank: PlayingCard.PlayingCard.Rank.numeric(3)) PlayingCard(suit: PlayingCard.PlayingCard.Suit.hearts, rank: PlayingCard.PlayingCard.Rank.numeric(2)) PlayingCard(suit: PlayingCard.PlayingCard.Suit.clubs, rank: PlayingCard.PlayingCard.Rank.numeric(8)) PlayingCard(suit: PlayingCard.PlayingCard.Suit.clubs, rank: PlayingCard.PlayingCard.Rank.face("J")) PlayingCard(suit: PlayingCard.PlayingCard.Suit.dimonds, rank: PlayingCard.PlayingCard.Rank.numeric(4)) PlayingCard(suit: PlayingCard.PlayingCard.Suit.dimonds, rank: PlayingCard.PlayingCard.Rank.numeric(5)) |
육안으로 확인이 어렵습니다.
이를 보기 쉽게 출력되도록 수정해봅시다.
클래스 끝에 CustomStringConvertible 프로토콜 추가하여 함수를 구현해줍니다.
struct PlayingCard: CustomStringConvertible
{
var description: String {
return "\(rank)\(suit)"
}
...
}
위 코드 반영시 아래와 같이 출력됩니다.
acespades face("Q")dimonds numeric(6)dimonds numeric(3)hearts numeric(8)clubs numeric(9)dimonds face("Q")clubs numeric(9)hearts numeric(2)dimonds acedimond |
Suit와 Rank 모두 CustomStringConvertible 프로토콜을 추가해줍니다.
enum Suit: String, CustomStringConvertible {
var description: String {
return "\(self.rawValue)"
}
...
}
enum Rank: CustomStringConvertible {
var description: String {
switch self {
case .ace:
return "Ace"
case .face(let kind):
return kind
case .numeric(let pips):
return "\(pips)"
}
}
}
위 코드를 적용한 결과값은 아래와 같습니다.
Q❤️ 4♦️ J♦️ 10♣️ 6♣️ J❤️ 4♣️ 3❤️ 5♠️ 5♦️ |
'프로그래밍 > 스탠포드 iOS 11' 카테고리의 다른 글
[CHAPTER 6] 멀티터치 (2) (0) | 2022.01.11 |
---|---|
[CHAPTER 6] 멀티터치 (0) | 2022.01.07 |
[CHAPTER 5] 뷰에 나타내기 (3) (0) | 2022.01.05 |
[CHAPTER 5] 뷰에 나타내기 (2) (0) | 2022.01.03 |
[CHAPTER 5] 뷰에 나타내기 (0) | 2022.01.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- ios
- Git
- 콜백
- static
- 콜백함수
- Windows
- 윈도우
- Stanford
- 설치
- 개발환경
- xcode
- 비트 확인
- Java
- C++
- 맴버함수
- 안드로이드
- swift
- 안드로이드 스튜디오
- 깃
- Android
- Windows10
- 자바
- 다운로드
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함