枚举

作者: 暖光照 | 来源:发表于2016-10-30 03:53 被阅读0次

与C或者Objective-C不同的是,在Swift语言中枚举类型的成员初始的时候不会被默认赋值成整数值,不同的枚举成员将要用什么类型以及赋值什么值都是可以自己控制的,可以在定义CompassPoint这个枚举的时候指定.

枚举语法

//多行定义
enum CompassPoint {
    case North
    case South
    case East
    case West
} 
//一行定义
enum Plant{
    case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
} 
//带关联值
enum Barcode {
    case UPCA(Int, Int, Int)
    case QRCode(String)
}
//带原始值的,原始值必须不同,否则编译报错
enum ASCIIControlCharacter: Character {
case Tab = "\t"
case LineFeed = "\n"
case CarriageReturn = "\r"
}
//原始值是整数类型,那么当其他枚举成员没有设置原始值的时候,他们的原始值是这个整型原始值自增长设置的。venus=2....
enum Planet: Int {
case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}

使用枚举

每个枚举的定义都是定义了一个全新的类型,就像Swfit中的其他的类型一样

var directionToHead = CompassPoint.West  //directionToHead被推断为CompassPoint类型
directionToHead = .East //directionToHead已经为CompassPoint类型,可以简写
  
//使用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 the skies are blue")
}
// 输出"Watch out for penguins”   

//带关联值的
switch productBarcode {
     //获取关联值,定义为不可变let
     case .UPCA(let numberSystem, let identifier, let check):
         print("UPC-A with value of \(numberSystem), \(identifier), \(check).")
     //将关联值定义为可变var
     case .QRCode(var productCode):
         productCode = "abcdefghijklmnop"
         print("QR code with value of \(productCode).")
 }
    

 switch productBarcode {
     //关联的所有值都被当做不可变变量
     case let .UPCA(numberSystem, identifier, check):
         print("UPC-A with value of \(numberSystem), \(identifier), \(check).")
     case let .QRCode(productCode):
         print("QR code with value of \(productCode).")
 }

//通过枚举成员的toRaw()方法来获取他的原始值
let earthsOrder = Planet.Earth.rawValue
print(earthsOrder)//3
//fromRaw()方法来尝试通过一个原始值来寻找他所对应的枚举成员
let possiblePlanet = Planet(rawValue: 7)
print(possiblePlanet)//Optional(SwiftDemo.Planet.Uranus)
//原始值没有对应枚举成员的话返回nil
let possiblePlanet = Planet(rawValue: 9)
print(possiblePlanet)//nil

相关文章

  • 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 {...

  • C语言基础 之 枚举类型

    枚举类型 枚举类型: 列出所有可能的值 枚举类型的定义 枚举类型定义的一般格式:enum 枚举类型名 {枚举值表}...

  • 枚举

    枚举 Why:为什么需要枚举 What:枚举是什么; How:枚举怎么实现 When:枚举什么时候使用 Where...

  • 枚举的概念及应用

    一、枚举的概念 二、枚举类型的定义 三、枚举变量的定义 四、枚举使用的注意 五、枚举变量的基本操作 五、枚举变量的...

网友评论

      本文标题:枚举

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