【Swift学习笔记】——enumeration枚举类型遵循协议protocol

来源:互联网 发布:华为交换机telnet端口 编辑:程序博客网 时间:2024/06/11 16:55
Apple官方文档:The Swift Programming Language
Protocols and Extensions一节的小节练习,要求自行定义一个enumeration枚举类型,并且遵循ExampleProtocol协议:
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. protocol ExampleProtocol {  
  2.     var simpleDescription: String { get }  
  3.      mutating func adjust()  
  4. }  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. protocol ExampleProtocol {  
  2.     var simpleDescription: String { get }  
  3.      mutating func adjust()  
  4. }  

在网上找了好久,都不知道怎样实现,最后参照一篇帖子(http://forums.macrumors.com/showthread.php?t=1740890),最终实现如下:
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. enum EnumConformToProtocol: ExampleProtocol {  
  2.     case First(String), Second(String), Third(String)  
  3.       
  4.     var simpleDescription: String {  
  5.         get {  
  6.             switch self {  
  7.             case let .First(text):  
  8.                 return text  
  9.             case let .Second(text):  
  10.                 return text  
  11.             case let .Third(text):  
  12.                 return text  
  13.             default:  
  14.                 return "get error"  
  15.             }  
  16.         }  
  17.         set {  
  18.             switch self {  
  19.             case let .First(text):  
  20.                 self = .First(newValue)  
  21.             case let .Second(text):  
  22.                 self = .Second(newValue)  
  23.             case let .Third(text):  
  24.                 self = .Third(newValue)  
  25.             }  
  26.         }  
  27.     }  
  28.     mutating func adjust() {  
  29.         switch self {  
  30.         case let .First(text):  
  31.             self = .First(text + " (first case adjusted)")  
  32.         case let .Second(text):  
  33.             self = .Second(text + " (second case adjusted)")  
  34.         case let .Third(text):  
  35.             self = .Third(text + " (third case adjusted)")  
  36.         }  
  37.     }  
  38. }  
  39.   
  40. var enumConformToProtocolTest = EnumConformToProtocol.First("FirstVal")  
  41. enumConformToProtocolTest.simpleDescription  
  42. enumConformToProtocolTest.adjust()  
  43. enumConformToProtocolTest.simpleDescription  
  44.   
  45. enumConformToProtocolTest = EnumConformToProtocol.Third("ThirdVal")  
  46. enumConformToProtocolTest.simpleDescription  
  47. enumConformToProtocolTest.adjust()  
  48. enumConformToProtocolTest.simpleDescription  
  49.   
  50.   
  51. var e = EnumConformToProtocol.Second("Hello")  
  52. var text = e.simpleDescription  
  53. e.simpleDescription = "Adios"  
  54. text = e.simpleDescription  
  55. e.adjust()  
  56. text = e.simpleDescription  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. enum EnumConformToProtocol: ExampleProtocol {  
  2.     case First(String), Second(String), Third(String)  
  3.       
  4.     var simpleDescription: String {  
  5.         get {  
  6.             switch self {  
  7.             case let .First(text):  
  8.                 return text  
  9.             case let .Second(text):  
  10.                 return text  
  11.             case let .Third(text):  
  12.                 return text  
  13.             default:  
  14.                 return "get error"  
  15.             }  
  16.         }  
  17.         set {  
  18.             switch self {  
  19.             case let .First(text):  
  20.                 self = .First(newValue)  
  21.             case let .Second(text):  
  22.                 self = .Second(newValue)  
  23.             case let .Third(text):  
  24.                 self = .Third(newValue)  
  25.             }  
  26.         }  
  27.     }  
  28.     mutating func adjust() {  
  29.         switch self {  
  30.         case let .First(text):  
  31.             self = .First(text + " (first case adjusted)")  
  32.         case let .Second(text):  
  33.             self = .Second(text + " (second case adjusted)")  
  34.         case let .Third(text):  
  35.             self = .Third(text + " (third case adjusted)")  
  36.         }  
  37.     }  
  38. }  
  39.   
  40. var enumConformToProtocolTest = EnumConformToProtocol.First("FirstVal")  
  41. enumConformToProtocolTest.simpleDescription  
  42. enumConformToProtocolTest.adjust()  
  43. enumConformToProtocolTest.simpleDescription  
  44.   
  45. enumConformToProtocolTest = EnumConformToProtocol.Third("ThirdVal")  
  46. enumConformToProtocolTest.simpleDescription  
  47. enumConformToProtocolTest.adjust()  
  48. enumConformToProtocolTest.simpleDescription  
  49.   
  50.   
  51. var e = EnumConformToProtocol.Second("Hello")  
  52. var text = e.simpleDescription  
  53. e.simpleDescription = "Adios"  
  54. text = e.simpleDescription  
  55. e.adjust()  
  56. text = e.simpleDescription  

运行结果截图:



0 0