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:
- Protocol Exampleprotocol {
- var simpledescription:string {Get}
- Mutating func adjust ()
- }
Enumerate inheritance Protocols
- Enum Enumconformtoprotocol:exampleprotocol {
- Case first (string), Second (String), third (string)
- var simpledescription:string {
- get {
- switch self {
- Case let . First (text):
- return text
- Case let . Second (text):
- return text
- Case let . Third (text):
- return text
- Default:
- return "Get Error"
- }
- }
- set {
- switch self {
- Case let . First (text):
- Self = . First (newvalue)
- Case let . Second (text):
- Self = . Second (newvalue)
- Case let . Third (text):
- Self = . Third (newvalue)
- }
- }
- }
- Mutating func adjust () {
- switch self {
- Case let . First (text):
- Self = . First (text + "(first case adjusted)")
- Case let . Second (text):
- Self = . Second (text + "(Second case adjusted)")
- Case let . Third (text):
- Self = . Third (text + "(third case adjusted)")
- }
- }
- }
- var enumconformtoprotocoltest = Enumconformtoprotocol. First ("Firstval")
- Enumconformtoprotocoltest. Simpledescription
- Enumconformtoprotocoltest. Adjust ()
- Enumconformtoprotocoltest. Simpledescription
- Enumconformtoprotocoltest = Enumconformtoprotocol. Third ("Thirdval")
- Enumconformtoprotocoltest. Simpledescription
- Enumconformtoprotocoltest. Adjust ()
- Enumconformtoprotocoltest. Simpledescription
- var e = Enumconformtoprotocol. Second ("Hello")
- var text = e. simpledescription
- E. simpledescription = "Adios"
- Text = e. simpledescription
- E. Adjust ()
- Text = e. simpledescription
Swift-Enum Inheritance protocol