学习swift从青铜到王者之swift枚举07
空枚举
//空枚举
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")
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).")
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的更多相关文章
- 学习swift从青铜到王者之swift属性09
1.结构体常量和类常量的存储属性 let p1 = Person1() //p1.age = 88 不允许修改 //p11.name = "yhx1" 不允许修改 var p11 ...
- 学习swift从青铜到王者之swift闭包06
语法表达式 一般形式:{ (parameters) -> returnType in statements } 这里的参数(parameters),可以是in-out(输入输出参数),但不能设定 ...
- 学习swift从青铜到王者之swift结构体和类08
定义 // 定义类 class StudentC{ } // 定义结构体 struct StudentS{ } 定义存储属性 // 定义类 class StudentC{ var name:Strin ...
- 学习swift从青铜到王者之Swift语言函数05
1.定义一个函数以及调用 //一,定义一个无参无返回值函数 func fun1(){ print("this is first function") } fun1() 2.定义一个 ...
- 学习swift从青铜到王者之Swift控制语句04
1 if语句基本用法 if boolean_expression { /* 如果布尔表达式为真将执行的语句 */ } 如果布尔表达式为 true,则 if 语句内的代码块将被执行.如果布尔表达式为 f ...
- 学习swift从青铜到王者之Swift集合数据类型03
1 数组的定义 var array1 = [,,,] var array2: Array = [,,,] var array3: Array<Int> = [,,,] var array4 ...
- 学习swift从青铜到王者之swift基础部分01
1.1 变量和常量 var 变量名称 = 值(var可以修改) let 常量名称 = 值(let不可以修改) 1.2 基本数据类型 整数类型和小数类型 两种基本数据类型不可以进行隐式转换 var in ...
- 学习swift从青铜到王者之字符串和运算符02
1 字符和字符串初步 var c :Character = "a" 2 构造字符串 let str1 = "hello" let str2 = " ...
- 学习Android从青铜到王者之第一天
1.Android四层架构 一.Linux Kernel 二.Libraries和Android Runtime 三.Application Framework 四.Applications 一.Li ...
随机推荐
- mysql 中modify和change区别(以及使用modify修改字段名称报错)
使用modify修改字段报错如下: mysql> alter table student modify name sname char(16);ERROR 1064 (42000): You h ...
- CPP-基础:inline
背景: 在C&C++中 一.inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 表达式形式的宏定义一例: #define ExpressionNam ...
- Hibernate-01 入门
学习任务 Hibernate开发环境的搭建 使用Hibernate对单表进行增删改操作 使用Hibernate按照数据表主键查询 关于Hibernate 简介 Hibernate的创始人Gavin K ...
- MySQL中外键删除、更新
MySQL支持外键的存储引擎只有InnoDB..在创建外键的时候,可以指定在删除.更新父表时,对子表进行的相应操作,包括RESTRICT.NO ACTION.SET NULL和CASCADE. 其 ...
- 硬盘写入 iso
https://www.jb51.net/softjc/508796.html WinImage 正确操作是要有两个or以上-硬盘. 这样才能写入你要装的操作系统 测试或者安装
- FileZilla Server安装配置教程
1. FileZilla官网下载FileZilla Server服务器,目前最新版本为0.9.53. 2. 安装FileZilla服务器.除以下声明的地方外,其它均采用默认模式,如安装路径等. 2.1 ...
- set容器几个关键函数
set在OI中非常好用,归纳几种常见的功能qwq #include<iostream> #include<cstdio> #include<set> //set容器 ...
- 2. CHARACTER_SETS
2. CHARACTER_SETS CHARACTER_SETS表提供有关可用字符集的信息. 下表中的SHOW Name值对应于SHOW CHARACTER SET语句的列名. INFORMATION ...
- int内部方法释义
python基本数据类型包括:int.str.list.tuple.dict.bool.set(),一切事物都是对象,对象由类创建 1. bit_length:返回数字占用的二进制最小位数 def b ...
- html5的导出表格功能
最近遇到一个需要导出表格的需求,研究了一下nodeJs的excel模块及好多其他的插件,发现还是蛮复杂的,由于项目对于表格的要求不高,因此同事推荐了一种h5的表格导出生成方法,比较简单,在此记录一下 ...