空枚举

//空枚举
enum SomeEnumeration {
// enumeration definition goes here
}

枚举基本类型

//枚举基本类型
enum CompassPoint {
case north
case south
case east
case west
}

简写

//简写
enum Planet {
case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
}

枚举语法

//枚举语法
func testEnumerationSyntax() {
//使用
var directionToHead = CompassPoint.west
//可不写 前面的枚举名
directionToHead = .east
print("\(directionToHead)") /* print
east
*/
}

枚举匹配

//枚举匹配
func testMatchingEnumeration() {
var directionToHead = CompassPoint.south
//if匹配
if directionToHead == CompassPoint.south {
directionToHead = .east
}
//switch匹配
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")
default:
print("default")
} /* print
Where the sun rises
*/
}

关联值

//关联值
func testAssociatedValues() {
//枚举可以和结构体类型的数据关联使用
enum Barcode {
case upca(Int, Int, Int, Int)
case qrCode(String)
}
// 初始化
var productBarcode = Barcode.upca(, , , )
productBarcode = .qrCode("ABCDEFGHIJKLMNOP") //匹配
switch productBarcode {//有警告 要求是常数
case .upca(let numberSystem, let manufacturer, let product, let check):
print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
case .qrCode(let productCode):
print("QR code: \(productCode).")
} //可以不写let
switch productBarcode {
case let .upca(numberSystem, manufacturer, product, check):
print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
case let .qrCode(productCode):
print("QR code: \(productCode).")
} /* print
QR code: ABCDEFGHIJKLMNOP.
QR code: ABCDEFGHIJKLMNOP. */
}

原始值

//原始值
func testRawValues() {
enum ASCIIControlCharacter: Character {
case tab = "\t"
case lineFeed = "\n"
case carriageReturn = "\r"
} //隐式分配原始值
enum Planet: Int {
case mercury = , venus, earth, mars, jupiter, saturn, uranus, neptune
} //原始值为属性名转换
enum CompassPoint: String {
case North, South, East, West
} print("\(Planet.earth.rawValue)")
print("\(CompassPoint.West.rawValue)") // 通过原始值初始化
let possiblePlanet = Planet(rawValue: )
print("\(possiblePlanet)")
let positionToFind =
print("\(possiblePlanet)") // 当原始值不匹配时,返回为nil
if let somePlanet = Planet(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 \(positionToFind)")
} /* print 3
West
Optional(Swift_枚举.(testRawValues () -> ()).(Planet #1).uranus)
Optional(Swift_枚举.(testRawValues () -> ()).(Planet #1).uranus)
There isn't a planet at position 9 */
}

枚举循环

//枚举循环
func testRecursiveEnumerations() {
//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) // 乘
} // 函数使用
func evaluate(_ expression: ArithmeticExpression) -> Int {
switch expression {
case .number(let value):
return value
case .addition(let left, let right):
return evaluate(left) + evaluate(right)
case .multiplication(let left, let right):
return evaluate(left) * evaluate(right)
}
} // evaluate (5 + 4) * 2
let five = ArithmeticExpression.number()
let four = ArithmeticExpression.number()
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number())
print(evaluate(product)) /* print
18
*/
}

学习swift从青铜到王者之swift枚举07的更多相关文章

  1. 学习swift从青铜到王者之swift属性09

    1.结构体常量和类常量的存储属性 let p1 = Person1() //p1.age = 88 不允许修改 //p11.name = "yhx1" 不允许修改 var p11 ...

  2. 学习swift从青铜到王者之swift闭包06

    语法表达式 一般形式:{ (parameters) -> returnType in statements } 这里的参数(parameters),可以是in-out(输入输出参数),但不能设定 ...

  3. 学习swift从青铜到王者之swift结构体和类08

    定义 // 定义类 class StudentC{ } // 定义结构体 struct StudentS{ } 定义存储属性 // 定义类 class StudentC{ var name:Strin ...

  4. 学习swift从青铜到王者之Swift语言函数05

    1.定义一个函数以及调用 //一,定义一个无参无返回值函数 func fun1(){ print("this is first function") } fun1() 2.定义一个 ...

  5. 学习swift从青铜到王者之Swift控制语句04

    1 if语句基本用法 if boolean_expression { /* 如果布尔表达式为真将执行的语句 */ } 如果布尔表达式为 true,则 if 语句内的代码块将被执行.如果布尔表达式为 f ...

  6. 学习swift从青铜到王者之Swift集合数据类型03

    1 数组的定义 var array1 = [,,,] var array2: Array = [,,,] var array3: Array<Int> = [,,,] var array4 ...

  7. 学习swift从青铜到王者之swift基础部分01

    1.1 变量和常量 var 变量名称 = 值(var可以修改) let 常量名称 = 值(let不可以修改) 1.2 基本数据类型 整数类型和小数类型 两种基本数据类型不可以进行隐式转换 var in ...

  8. 学习swift从青铜到王者之字符串和运算符02

    1 字符和字符串初步  var c :Character = "a" 2 构造字符串  let str1 = "hello" let str2 = " ...

  9. 学习Android从青铜到王者之第一天

    1.Android四层架构 一.Linux Kernel 二.Libraries和Android Runtime 三.Application Framework 四.Applications 一.Li ...

随机推荐

  1. web测试需要注意点

  2. centos下安装nodejs的三种种方式

    方法一:源码包安装 官网下载 centos下载最新版10.9 https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz mkdir / ...

  3. python爬虫---从零开始(三)Requests库

    1,什么是Requests库 Requests是用python语言编写,基于urllib,采用Apache2 Licensed 开源协议的HTTP库. 它比urllib更加方便,可以节约我们大量的工作 ...

  4. SFM作业

    代码:https://github.com/jianxiongxiao/SFMedu PPT:http://3dvision.princeton.edu/courses/SFMedu/slides.p ...

  5. ORA-03113: end-of-file on & ORA-07445

    --------------ORA-03113: end-of-file on-------------- SQL> show parameter background_dump; NAME T ...

  6. luogu 1113 杂务--啥?最长路?抱歉,我不会

    P1113 杂务 题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务 ...

  7. ES6中Generator

    ES6中Generator Generator是ES6一个很有意思的特性,也是不容易理解的特性.不同于let/const提供了块级作用域这样明显的目的,这玩意儿被搞出来到底是干嘛的? 首先我们需要明确 ...

  8. 前端面试绝对会考的JS问题!【已经开源】

    写在前面 [前端指南]前端面试库已经开源,正在完善之中 [x] css问题 [x] html问题 [x] javascript问题 github地址 https://github.com/nanhup ...

  9. 条款30:透彻了解inline的里里外外(understand the ins and outs of inlining)

    NOTE: 1.将大多数inline限制在小型 被频繁调用的函数身上.这可使日后的调试过程和二进制升级(binary upgradability)更容易,也可使潜在的代码膨胀问题最小化, 使程序的速度 ...

  10. C语言学习11

    直接插入排序 //直接插入排序 #include <stdio.h> void main() { ], i; int insort(int a[], int n); printf(&quo ...