]> Zhao Yanbai Git Server - acecode.git/commitdiff
enumerations
authorAce <acezhao@tencent.com>
Wed, 22 Jun 2016 04:53:20 +0000 (12:53 +0800)
committerAce <acezhao@tencent.com>
Wed, 22 Jun 2016 04:53:20 +0000 (12:53 +0800)
learn/AcePlay/AcePlay.playground/Pages/Enumerations.xcplaygroundpage/Contents.swift [new file with mode: 0644]
learn/AcePlay/AcePlay.playground/contents.xcplayground
learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate

diff --git a/learn/AcePlay/AcePlay.playground/Pages/Enumerations.xcplaygroundpage/Contents.swift b/learn/AcePlay/AcePlay.playground/Pages/Enumerations.xcplaygroundpage/Contents.swift
new file mode 100644 (file)
index 0000000..34b3a91
--- /dev/null
@@ -0,0 +1,104 @@
+//: [Previous](@previous)
+
+import UIKit
+
+enum CompassPoint {
+    case North
+    case East
+    case South
+    case West
+}
+
+var directionToHead = CompassPoint.North
+directionToHead = .East
+
+switch directionToHead {
+    case .North: print("Losts of Plantes Have a North")
+    case .East : print("Where the Sun Rises")
+    case .South: print("Watch Out for Penguins")
+    case .West : print("Where the Skies are Blue")
+}
+
+
+// Associated Values
+printLine("Enumeration Associated Values")
+enum BarCode {
+    case UPCA(Int, Int, Int, Int)
+    case QRCode(String)
+}
+
+var productBarCode = BarCode.UPCA(8, 88488, 66366, 2)
+productBarCode = .QRCode("QRCode-123456")
+
+switch productBarCode {
+case let .UPCA(numberSystem, manufacturer, product, check) :
+    print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check)")
+case let .QRCode(productCode) :
+    print("QR Code: \(productBarCode)")
+}
+
+
+printLine("RawValues")
+// Raw Values
+enum ASCIIControlCharacter: Character {
+    case Tab            = "\t"
+    case LineFeed       = "\n"
+    case CarriageReturn = "\r"
+}
+
+// Implicitly Assigned Raw Values
+// 整型从0开始自增1,每个项的rawValue不能相同
+enum Planet: Int {
+    case Mercury
+    case Venus
+    case Earth
+    case Mars
+    case Jupiter
+    case Saturn
+    case Uranus
+    case Netpune
+}
+
+print(Planet.Mercury, Planet.Mercury.rawValue)
+print(Planet.Mars, Planet.Mars.rawValue)
+print(Planet(rawValue: 5)!, Planet(rawValue: 5)!.rawValue)
+
+
+// String型,每项默认为枚举项的字符串值
+enum CompassPointString: String {
+    case North
+    case East
+    case Sourth
+    case West
+}
+
+
+print(CompassPointString.North, CompassPointString.North.rawValue)
+print(CompassPointString.East,  CompassPointString.East.rawValue)
+print(CompassPointString.Sourth,CompassPointString.Sourth.rawValue)
+print(CompassPointString.West,  CompassPointString.West.rawValue)
+
+
+printLine("Resursive Enumerations")
+// 不能忘记 indirect
+indirect enum Exp {
+    case Number(Int)
+    case Add(Exp, Exp)  // indirect 也可以放在case语句之前
+    case Mul(Exp, Exp)
+}
+
+
+let exp = Exp.Mul(Exp.Add(Exp.Number(2), Exp.Number(5)), Exp.Number(7))
+
+func evaluateExp(exp: Exp) -> Int {
+    switch exp {
+    case let .Number(n):
+        return n
+    case let .Add(a, b) :
+        return evaluateExp(a) + evaluateExp(b)
+    case let .Mul(a, b):
+        return evaluateExp(a) * evaluateExp(b)
+    }
+}
+
+print(evaluateExp(exp))
index 90f71901a3641f8118265400399eb6a0eb8d99a1..04644e5717ffaad6190a50887613b8d25706545d 100644 (file)
@@ -4,5 +4,6 @@
         <page name='Functions'/>
         <page name='Basics'/>
         <page name='Closure'/>
+        <page name='Enumerations'/>
     </pages>
 </playground>
\ No newline at end of file
index 1bafc59b124ca6ef98ca7707abd10100e270d426..f12efa0bb7e9602b4f04d60e044dfebad40714b7 100644 (file)
Binary files a/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate and b/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate differ