swift 20 - Nested Types

Posted zzfx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 20 - Nested Types相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

深入浅出 Swift 中的 someany 关键字以及主关联类型(primary associated types)

swift Swift,Struct和Inheritance:如何平衡使用Struct和Value Types的意愿以及继承的需要?

Swift学习笔记 - 教程学习三 集合类型 (Collection Types)

nested friend

事务的传播行为解析(Transaction propagation: Required,RequiresNew,Nested)

node_modules/@types/node/index.d.ts(20,1):错误 TS1084:无效的“参考”指令语法