swift-枚举

//: 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
 

  • 其他关于枚举的博客
    • swift 枚举

你可能感兴趣的:(swift-枚举)