Swift 20-nested Types

Source: Internet
Author: User

Nested Types Just for the convenience of type integration and use

  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.