From: Ace Date: Wed, 22 Jun 2016 04:53:20 +0000 (+0800) Subject: enumerations X-Git-Url: http://zhaoyanbai.com/repos/FAQ?a=commitdiff_plain;h=f19f6380ca4860c4381407b81c03de9ff01c108f;p=acecode.git enumerations --- 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 index 0000000..34b3a91 --- /dev/null +++ b/learn/AcePlay/AcePlay.playground/Pages/Enumerations.xcplaygroundpage/Contents.swift @@ -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)) diff --git a/learn/AcePlay/AcePlay.playground/contents.xcplayground b/learn/AcePlay/AcePlay.playground/contents.xcplayground index 90f7190..04644e5 100644 --- a/learn/AcePlay/AcePlay.playground/contents.xcplayground +++ b/learn/AcePlay/AcePlay.playground/contents.xcplayground @@ -4,5 +4,6 @@ + \ No newline at end of file diff --git a/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate b/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate index 1bafc59..f12efa0 100644 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