码迷,mamicode.com
首页 > 编程语言 > 详细

swift 20 - Nested Types

时间:2018-04-08 13:18:13      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:span   使用   ceo   black   tput   turn   and   values   rip   

Nested Types 只是为了方便类型的整合和使用

 

  1. struct BlackjackCard {
  2. // nested Suit enumeration
  3. enum Suit: Character {
  4. case spades = "?", hearts = "?", diamonds = "?", clubs = "?"
  5. }
  6. // nested Rank enumeration
  7. enum Rank: Int {
  8. case two = 2, three, four, five, six, seven, eight, nine, ten
  9. case jack, queen, king, ace
  10. struct Values {
  11. let first: Int, second: Int?
  12. }
  13. var values: Values {
  14. switch self {
  15. case .ace:
  16. return Values(first: 1, second: 11)
  17. case .jack, .queen, .king:
  18. return Values(first: 10, second: nil)
  19. default:
  20. return Values(first: self.rawValue, second: nil)
  21. }
  22. }
  23. }
  24. // BlackjackCard properties and methods
  25. let rank: Rank, suit: Suit
  26. var description: String {
  27. var output = "suit is \(suit.rawValue),"
  28. output += " value is \(rank.values.first)"
  29. if let second = rank.values.second {
  30. output += " or \(second)"
  31. }
  32. return output
  33. }
  34. }

 

  1. let theAceOfSpades = BlackjackCard(rank: .ace, suit: .spades)
  2. print("theAceOfSpades: \(theAceOfSpades.description)")
  3. // Prints "theAceOfSpades: suit is ?, value is 1 or 11"

swift 20 - Nested Types

标签:span   使用   ceo   black   tput   turn   and   values   rip   

原文地址:https://www.cnblogs.com/feng9exe/p/8744221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!