Swift-Enum Inheritance protocol

Source: Internet
Author: User
Tags vars

Original Address link: http://blog.csdn.net/duanyipeng/article/details/32338575

Apple Official document: The Swift programming Language
The Protocols and Extensions section practice requires that you define a enumeration enumeration type yourself and follow the Exampleprotocol protocol:

  

    1. Protocol Exampleprotocol {
    2. var simpledescription:string {Get}
    3. Mutating func adjust ()
    4. }

Enumerate inheritance Protocols

  1. Enum Enumconformtoprotocol:exampleprotocol {
  2. Case first (string), Second (String), third (string)
  3. var simpledescription:string {
  4. get {
  5. switch self {
  6. Case let . First (text):
  7. return text
  8. Case let . Second (text):
  9. return text
  10. Case let . Third (text):
  11. return text
  12. Default:
  13. return "Get Error"
  14. }
  15. }
  16. set {
  17. switch self {
  18. Case let . First (text):
  19. Self = . First (newvalue)
  20. Case let . Second (text):
  21. Self = . Second (newvalue)
  22. Case let . Third (text):
  23. Self = . Third (newvalue)
  24. }
  25. }
  26. }
  27. Mutating func adjust () {
  28. switch self {
  29. Case let . First (text):
  30. Self = . First (text + "(first case adjusted)")
  31. Case let . Second (text):
  32. Self = . Second (text + "(Second case adjusted)")
  33. Case let . Third (text):
  34. Self = . Third (text + "(third case adjusted)")
  35. }
  36. }
  37. }
  38. var enumconformtoprotocoltest = Enumconformtoprotocol. First ("Firstval")
  39. Enumconformtoprotocoltest. Simpledescription
  40. Enumconformtoprotocoltest. Adjust ()
  41. Enumconformtoprotocoltest. Simpledescription
  42. Enumconformtoprotocoltest = Enumconformtoprotocol. Third ("Thirdval")
  43. Enumconformtoprotocoltest. Simpledescription
  44. Enumconformtoprotocoltest. Adjust ()
  45. Enumconformtoprotocoltest. Simpledescription
  46. var e = Enumconformtoprotocol. Second ("Hello")
  47. var text = e. simpledescription
  48. E. simpledescription = "Adios"
  49. Text = e. simpledescription
  50. E. Adjust ()
  51. Text = e. simpledescription

Swift-Enum Inheritance protocol

Related Article

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.