美文网首页
16-Swift枚举

16-Swift枚举

作者: 诠释残缺 | 来源:发表于2017-04-11 13:05 被阅读11次

枚举为一组相关的值定义了一个共同的类型,使你可以在你的代码中以类型安全的方式来使用这些值。
Swift 中的枚举更加灵活,不必给每一个枚举成员提供一个值。如果给枚举成员提供一个值(称为“原始”值),则该值的类型可以是 字符串,字符,或是一个整型值或浮点数。

1.枚举语法

//使用 enum 关键词来创建枚举并且把它们的整个定义放在一对大括号内:
enum SomeEnumeration {
    // 枚举定义放在这里
}

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 的类型可以在它被 CompassPoint 的某个值初始化时推断出来。一旦 directionToHead 被声明为 CompassPoint 类型,你可以使用
更简短的点语法将其设置为另一个 CompassPoint 的值

2.使用Switch语句匹配枚举值

directionToHead = .south
switch directionToHead {
case .north:
    print("Lots of planets have a north")
case .south:
    print("Watch out for penguins")
case .east:
    print("Where the sun rises")
case .west:
    print("Where th skies are blue")
}
// 打印 Watch out for penguins

let somePlanet = Planet.earth
switch somePlanet {
case .earth:
    print("Mostly harmless")
default:
    print("Not a safe place for humans")
}
// 打印 Mostly harmless

3.关联值

有时候能够把其他类型的关联值和成员值一起存储起来会很有用。这能让你连同成员值一起存储额外的自定义信息,并且你每次在代码中使用该枚举成员时,还可以修改这个关联值。
你可以定义 Swift 枚举来存储任意类型的关联值,如果需要的话,每个枚举成员的关联值类型可以各不相同。枚 举的这种特性跟其他语言中的可识别联合(discriminated unions),标签联合(tagged unions),或者变体(variants)相似。

enum Barcode {
    case upc(Int, Int, Int, Int)
    case qrCode(String)
}
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
productBarcode = .qrCode("ABCDEFGHIJKLMNOP")


//像先前那样,可以使用一个 switch 语句来检查不同的条形码类型。然而,这一次,关联值可以被提取出来作为 switch 语句的一部分。你可以在 switch 的 case 分支代码中提取每个关联值作为一个常量(用 let 前缀)或者 作为一个变量(用 var 前缀)来使用:
switch productBarcode {
case .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
switch productBarcode {
case let .upc(numberSystem, manufacturer, product, check):
    print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
case let .qrCode(productCode):
    print("QR code: \(productCode).")
}

4.原始值

作为关联值的替代选择,枚举成员可以被默认值(称为原始值)预填充,这些原始值的类型必须相同。

//ASCII码作为原始值的枚举
enum ASCIIControlCharacter: Character {
    case tab = "\t"
    case lineFeed = "\n"
    case carriageReturn = "\r"
}
print(ASCIIControlCharacter.tab)
// 打印 tab lineFeed carriageReturn


//当使用整数作为原始值时,隐式赋值的值依次递增 1 。如果第一个枚举成员没有设置原始值,其原始值将 为0。
//原始值的隐式赋值
enum Planet1: Int {
    case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}

enum CompassPoint1: String {
    case north, south, east, west
}

let earthsOrder = Planet1.earth.rawValue // 3
let sunsetDirection = CompassPoint1.west.rawValue // west

//使用原始值初始化枚举

let possiblePlanet = Planet1(rawValue: 7)
//possiblePlanet是Planet?类型,或者说是"可选的Planet" 可以为nil

let positionToFind = 11
if let somePlanet = Planet1(rawValue: positionToFind) {
    switch somePlanet {
    case .earth:
        print("地球")
    default:
        print("其他星球")
    }
} else {
     print("在\(positionToFind)位置上没有星球")}

5.递归枚举

递归枚举是一种枚举类型,它有一个或多个枚举成员使用该枚举类型的实例作为关联值。使用递归枚举时,编译器会插入一个间接层。你可以在枚举成员前加上 indirect 来表示该成员可递归。

enum ArithmeticExpression {
    case number(Int)
    indirect case addition(ArithmeticExpression, ArithmeticExpression)
    indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
//你也可以在枚举类型开头加上 indirect 关键字来表明它的所有成员都是可递归的:
//indirect enum ArithmeticExpression {
//    case number(Int)
//    case addition(ArithmeticExpression, ArithmeticExpression)
//    case multiplication(ArithmeticExpression, ArithmeticExpression)
//}

let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))

func evaluate(_ expression: ArithmeticExpression) -> Int {
    switch expression {
    case let .number(value):
        return value
    case let .addition(left, right):
        return evaluate(left) + evaluate(right)
    case let .multiplication(left, right):
        return evaluate(left) * evaluate(right)
    }
}
print(evaluate(product))

相关文章

  • 16-Swift枚举

    枚举为一组相关的值定义了一个共同的类型,使你可以在你的代码中以类型安全的方式来使用这些值。Swift 中的枚举更加...

  • 16-Swift泛型

    1.泛型 泛型可以将类型参数化,提高代码复用率,减少代码量 2.泛型函数赋值给变量 3.关联类型(Associat...

  • C#枚举及与枚举常数的转换

    1、枚举的定义 2、枚举绑定到comboBox 3、枚举常数转换为枚举string转枚举 数字值转枚举

  • Swift 基础笔记 - 枚举

    枚举 OC定义和使用枚举 Swift定义枚举类型 Swift判断枚举类型 枚举成员类型

  • 枚举类

    1.枚举类型的定义: 枚举类型定义的一般形式为 enum 枚举名{//枚举值表枚举值1;枚举值2;...} 在枚举...

  • 10、枚举与闭包

    枚举 枚举和普通类相比有什么优势 枚举类型、枚举名称与枚举值 枚举的比较运算 两个枚举之间可以使用等值比较(==)...

  • Swift与OC的语法简单对比(常用语法二)

    20- 枚举,枚举原始值,枚举相关值,switch提取枚举关联值 Swift枚举: Swift中的枚举比OC中的枚...

  • Swift 2 学习笔记 10.枚举

    课程来自慕课网liuyubobobo老师 枚举 枚举基础 枚举之原始值 枚举之关联值 枚举递归

  • swift3语法(八)

    枚举/结构体 枚举 定义形式enum 枚举名 { 枚举值} // 例如enum CompassPoint {...

  • 16-Swift错误处理

    错误处理是响应错误以及从错误中恢复的过程。swift提供了在运行对可恢复错误抛出、捕获、传送和操作的高级支持(错误...

网友评论

      本文标题:16-Swift枚举

      本文链接:https://www.haomeiwen.com/subject/foqeattx.html