8.枚举

enum Point {

case North

case South

}


var d = Point.North

d = .South

switch d {

case .North:

case .South:

}


switch d {

case .North:

default:

}


enum Code {

case UPCA(Int, Int, Int, Int)

case QRCode(String)

}

var pro = Code.UPCA(8, 85909, 51226, 3)

pro = .QRCode("ABCDEFGHIJKLMNOP")

switch pro {

case .UPCA(let a, let b, let c, let d):

print("UPC-A: \(a), \(b), \(c), \(d).")

case .QRCode(let str):

print("QR code: \(str).")

}

switch pro {

case let .UPCA(a, b, c, d):

print("UPC-A: \(a), \(b), \(c), \(d).")

case var .QRCode(str):

print("QR code: \(str).")

}


enum Point {

case North  = 1

case South

}

var a = Point.North.rawValue   //1

var b = Week(rawValue: 2)      //South




递归函数

indirect enum Ari {

case Num(Int)

case Add(Ari, Ari)

case Mul(Ari, Ari)

}

func eva_f(ari:Ari) -> Int {

switch ari {

case let .Num(a):

return a

case let .Add(a, b):

return eva_f(a) + eva_f(b)

case let .Mul(a, b):

return eva_f(a) * eva_f(b)

}

}

//(3+4)*6

var a = eva_f(Ari.Mul(Ari.Add(Ari.Num(3),Ari.Num(4)), Ari.Num(6)))

你可能感兴趣的:(8.枚举)