//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
//枚舉
// 計算屬性
// 實類方法
// 初始化
// 類擴展 extend
// 協議方法等
//枚舉語法
enum CompassPoint {
case north
case south
case east
case west
}
// 也可以寫在一行
enum Planet {
case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}
// 枚舉賦值
var directionToHead = CompassPoint.west
// 當已知 變量為枚舉類型時, 可以直接用 點語法來改變變量的值
directionToHead = .east
//關聯值 枚舉
// 對枚舉項 進行擴充一個附加的信息.
enum Barcode{
case upc(Int, Int, Int, Int)
case qrCode(String)
}
var productBarcode = Barcode.upc(8, 85909, 23987,3)
productBarcode = .qrCode("adfadfaf")
switch productBarcode {
case Barcode.upc(let numberSystem, let manufacturer, let product, let check):
print("UPC : \(numberSystem), \(manufacturer), \(product), \(check))")
case .qrCode(let productCode):
print("QR code \(productCode)")
}
//把 let 和 var 提取出來 放到 case name 前面
switch productBarcode {
case let .upc(numberSystem, manufacturer, product, check):
print("UPC : \(numberSystem), \(manufacturer), \(product), \(check))")
case let .qrCode(productCode):
print("QR code \(productCode)")
}
//枚舉中的類型可以不一樣
enum some{
case afadf
case second(Int,Int)
}
//原始值 枚舉 在編譯的時候就已經有值了
// 要明確申明 枚舉的類型
enum ASIIControlCharacter:Character{
case tab = "\t"
case lineFeed = "\n"
case carriageReturn = "\r"
}
// 含蓄的原始值類型 枚舉
// mercury 明確設值為 1, 系統會自動推算出后面的值.如 venus = 2. earch = 3 等
enum PlanetOne:Int{
case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}
// 當原始值為 String 類型時, case 項都沒有設置默認值, 則 case 的默認值為自己本身 如 north = "north" south = "south" 等
enum CompassPointOne: String {
case north, south, east, west
}
// 獲取 原始值枚舉的 原始值
let earthOrder = PlanetOne.earth.rawValue // = 3
let sunsetDiraction = CompassPointOne.west.rawValue// = "west"
// 用原始值來初始化 枚舉類型數據.
//原始值 為 5, 這里的原始值必須是我們定義 枚舉型所有原始值中的一個, 這樣才會返回一個正確的枚舉類型. 如果是非法的原始值 則返回 nil , 所有用原始值來初始化枚舉類型 結果是一個可選值
let possiblePlanet = PlanetOne(rawValue: 5)
// 這里用原始值 "north" 來初始化
let possibleCompass = CompassPointOne(rawValue: "north")
//用原始值初始化枚舉類型 綜合使用
let positionTofind = 11 // 判斷太陽系 第11顆行星是什么? 實際上 沒有11 這個行星
if let somePlanet = PlanetOne(rawValue: positionTofind){
switch somePlanet {
case .earth:
print("Mostly harmless")
default:
print("Not a safe place for humans")
}
} else{
print("There isn't a planet at position")
}
// 打印:There isn't a planet at position
//遞歸枚舉
enum ArithmeticExpression{
case number(Int)
indirect case addition(ArithmeticExpression,ArithmeticExpression)
indirect case mutiplication(ArithmeticExpression,ArithmeticExpression)
}
// 上面的枚舉也可以寫作
indirect enum ArithmeticExpressionOne{
case number(Int)
case addition(ArithmeticExpression,ArithmeticExpression)
case mutiplication(ArithmeticExpression,ArithmeticExpression)
}
//如: 我們要實現表達式 (5+4)*2
let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.mutiplication(sum, ArithmeticExpression.number(2))
func evalute(_ expression:ArithmeticExpression) ->Int{
switch expression {
case let .number(value):
return value
case let .addition(left, right):
return evalute(left) + evalute(right)
case let .mutiplication(left,right):
return evalute(left) * evalute(right)
}
}
print(evalute(product))
//上面這個例子很有意思
//枚舉中 含有方法
enum State {
case ready, executing, finished
func keyPath() -> String {
switch self {
case .ready:
return "isReady"
case .executing:
return "isExecuting"
case .finished:
return "isFinished"
}
}
}
let currentState = State.ready