枚举以及可选项
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
//: [Previous](@previous)
|
||||
|
||||
import Foundation
|
||||
|
||||
var greeting = "Hello, playground"
|
||||
|
||||
//: #### 枚举的基本用法
|
||||
// 枚举的基本定义
|
||||
enum Direction {
|
||||
case north
|
||||
case south
|
||||
case east
|
||||
case west
|
||||
}
|
||||
|
||||
// 枚举值的访问
|
||||
var dir = Direction.north
|
||||
dir = Direction.east
|
||||
dir = .north
|
||||
print(dir)
|
||||
|
||||
//: #### 关联值(Associated Values)
|
||||
// 将枚举成员的值与其它类型的关联存储在一起
|
||||
enum Score {
|
||||
case points(Int)
|
||||
case grade(Character)
|
||||
}
|
||||
var score = Score.points(96)
|
||||
score = .grade("A")
|
||||
|
||||
//: #### 原始值(Raw Value)
|
||||
// 枚举成员可以使用相同类型的没人值预选关联,这个默认值就是原始值
|
||||
|
||||
enum PokerSuit : Character { // 原始值类型都为字符类型
|
||||
case spade = "♠️"
|
||||
case heart = "♥️"
|
||||
case dismand = "♦️"
|
||||
case club = "♣️"
|
||||
}
|
||||
|
||||
var suit = PokerSuit.spade
|
||||
print(suit)
|
||||
print(suit.rawValue) // 打印原始值
|
||||
|
||||
//: ##### 隐式原始值
|
||||
/*:
|
||||
隐式原始值
|
||||
如果枚举的原始值是Int、String,Swift会自动分配原始值
|
||||
- Int类型的原始值,如果第一项没有值,默认是从0开始往下依次增加。如果有值,就在该值的基础上依次往下增加
|
||||
- String类型的原始值,默认就是枚举成员的名称
|
||||
*/
|
||||
|
||||
|
||||
//: #### MemoryLayout 用于获取数据类型占用的内存大小
|
||||
/*:
|
||||
MemoryLayout中size、stride以及alignment之间的区别
|
||||
- size: 实际占用空间的大小. Size is the number of bytes to read from the pointer to reach all the data.
|
||||
- stride: 分配占用空间的大小。Stride is the number of bytes to advance to reach the next item in the buffer.The stride determines the distance between two elements, which will be greater than or equal to the size.
|
||||
- alignment:对齐参数。The alignment of a struct type is the maximum alignment out of all its properties.Alignment is the the “evenly divisible by” number that each instance needs to be at.
|
||||
|
||||
*/
|
||||
var age = 18
|
||||
MemoryLayout.size(ofValue: age)
|
||||
MemoryLayout.alignment(ofValue: age)
|
||||
MemoryLayout.stride(ofValue: age)
|
||||
|
||||
enum Password {
|
||||
case other
|
||||
case number(Int, Int, Int, Int)
|
||||
}
|
||||
|
||||
MemoryLayout<Password>.size
|
||||
MemoryLayout<Password>.stride
|
||||
MemoryLayout<Password>.alignment
|
||||
|
||||
/*:
|
||||
1. Puppy的对其参数是8(Int类型最大)
|
||||
2. 每个成员变量的对其参数必须都是8
|
||||
3. Puppy成员在内存中的存储如下所示
|
||||
|
||||
-----------------------------------------------------------------
|
||||
|...|...|...|...|...|...|...|...|...| | | | | | | |
|
||||
|...|...|...|...|...|...|...|...|...| | | | | | | |
|
||||
|...|...|...|...|...|...|...|...|...| | | | | | | |
|
||||
-----------------------------------------------------------------
|
||||
Int Bool
|
||||
|-----------> <----------|---|
|
||||
|
||||
// Size is the number of bytes to read from the pointer to reach all the data.
|
||||
|-------------> size <--------------|
|
||||
size = 9
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
struct Puppy {
|
||||
let age: Int
|
||||
let isTrained: Bool
|
||||
} // Int, Bool
|
||||
|
||||
MemoryLayout<Puppy>.size
|
||||
MemoryLayout<Puppy>.stride
|
||||
MemoryLayout<Puppy>.alignment
|
||||
|
||||
|
||||
/*:
|
||||
1. AlternatePuppy的对其参数是8(Int类型最大)
|
||||
2. 每个成员变量的对其参数必须都是8
|
||||
3. AlternatePuppy成员在内存中的存储如下所示
|
||||
|
||||
-----------------------------------------------------------------
|
||||
|...| | | | | | | |...|...|...|...|...|...|...|...|
|
||||
|...| | | | | | | |...|...|...|...|...|...|...|...|
|
||||
|...| | | | | | | |...|...|...|...|...|...|...|...|
|
||||
-----------------------------------------------------------------
|
||||
|----------> Bool <----------|----------> Int <-----------|
|
||||
|
||||
// Size is the number of bytes to read from the pointer to reach all the data.
|
||||
|---------------------------> size <----------------------------|
|
||||
size = 16
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
struct AlternatePuppy {
|
||||
let isTrained: Bool
|
||||
let age: Int
|
||||
} // Bool, Int
|
||||
|
||||
MemoryLayout<AlternatePuppy>.size
|
||||
MemoryLayout<AlternatePuppy>.stride
|
||||
MemoryLayout<AlternatePuppy>.alignment
|
||||
|
||||
|
||||
//: [Next](@next)
|
@@ -0,0 +1,182 @@
|
||||
//: [Previous](@previous)
|
||||
|
||||
import Foundation
|
||||
|
||||
var greeting = "Hello, playground"
|
||||
|
||||
//: #### 可选项
|
||||
/*:
|
||||
可选项: 一般也叫可选类型,它允许将值设值为nil
|
||||
- 非可选项的变量一般不允许设值为nil的
|
||||
- 在类型名称的后面添加问号?来定义一个可选项
|
||||
|
||||
*/
|
||||
// 可选项定义示例
|
||||
var name: String? = "Zhang Mengxu"
|
||||
name = nil
|
||||
|
||||
// 可选项的默认值是 nil
|
||||
var age: Int?
|
||||
age = 10
|
||||
print(age)
|
||||
age = nil
|
||||
|
||||
//: #### 强制解包
|
||||
/*:
|
||||
强制解包
|
||||
可选项是对其他类型的一层包装,可以将它理解为盒子
|
||||
- 如果为nil,那么它是一个空盒子
|
||||
- 如果不为nil,那么盒子里边装的是:被包装类型的数据
|
||||
|
||||
如果要从可选项中取出被包装的数据,需要使用感叹号!
|
||||
如果对值为nil的可选项进行强制解包,将会产生运行错误。需要检测可选项是否包含值
|
||||
- 强制转化后判断是否为nil
|
||||
- 可选项绑定(Optional Binding)
|
||||
*/
|
||||
// 强制解包
|
||||
var age0: Int? = 10
|
||||
var ageInt: Int = age0!
|
||||
ageInt += 20
|
||||
|
||||
// 判断可选项是否包含值
|
||||
let number = Int("123")
|
||||
if number != nil {
|
||||
print("字符串转换成功,\(number!)")
|
||||
} else {
|
||||
print("字符串转换失败")
|
||||
}
|
||||
|
||||
// 可选项绑定(Optional Binding)
|
||||
/*:
|
||||
可以使用可选项绑定来判断可选项是否包含值
|
||||
如果包含就自动解包,把值赋给一个临时的常量(let)或者变量(var),并返回true,否则返回false
|
||||
*/
|
||||
if let number = Int( "123") {
|
||||
print("字符串转换整数成功:\(number) ")
|
||||
// number是强制解包之后的Int值
|
||||
// number作用域仅限于这个大括号
|
||||
}else {
|
||||
print("字符串转换整数失败")
|
||||
}
|
||||
|
||||
|
||||
enum Season: Int {
|
||||
case spring = 1, summer, autumn, winter
|
||||
}
|
||||
|
||||
if let s = Season(rawValue: 3) {
|
||||
switch s {
|
||||
case .spring:
|
||||
print("The season is spring")
|
||||
default:
|
||||
print("The season is other")
|
||||
}
|
||||
} else {
|
||||
print("no such season")
|
||||
}
|
||||
|
||||
// 可选项的并且关系, 可以用逗号进行表示
|
||||
// first且second存在,并且first < second && second < 100
|
||||
if let first = Int("4"), let second = Int("42"), first < second && second < 100 {
|
||||
print("-------------------")
|
||||
}
|
||||
|
||||
// where循环中使用可选项绑定
|
||||
var strs = ["10", "20", "abc", "-20"]
|
||||
var index = 0
|
||||
var sum = 0
|
||||
while let num = Int(strs[index]), num > 0 {
|
||||
sum += num
|
||||
index += 1
|
||||
}
|
||||
print(sum)
|
||||
|
||||
// 🈳合并运算符
|
||||
/*:
|
||||
空合并运算符 ??
|
||||
a ?? b
|
||||
- a是可选项
|
||||
- b是可选项或者不是可选项
|
||||
- b跟a的存储类型必须相同
|
||||
|
||||
a ?? b 的取值
|
||||
- 如果a不为nil,就返回a
|
||||
- 如果a为nil,就返回b
|
||||
- 如果b不是可选项,返回a时会自动解包
|
||||
*/
|
||||
|
||||
let a: Int? = 1
|
||||
let b: Int? = 2
|
||||
let c = a ?? b // c是Int? Optional(1)
|
||||
|
||||
let a: Int? = nil
|
||||
let b: Int? = 2
|
||||
let c = a ?? b // c是Int? Optional(2)
|
||||
|
||||
let a: Int? = 1
|
||||
let b: Int = 2
|
||||
let c = a ?? b // c是Int, 2
|
||||
|
||||
// ?? 与if的使用
|
||||
let a: Int? = nil
|
||||
let b: Int? = 2
|
||||
if let c = a ?? b {
|
||||
print(c)
|
||||
}
|
||||
|
||||
|
||||
//: #### guard语句
|
||||
/*:
|
||||
基本语法
|
||||
guard 条件 else {
|
||||
// do something
|
||||
退出当前作用域
|
||||
// return、break、throw
|
||||
}
|
||||
|
||||
- 当guard语句的条件为false时,会执行大括号里边的代码
|
||||
- 当guard语句的条件为true时,就会跳过guard语句
|
||||
|
||||
guard语句适合提前退出
|
||||
*/
|
||||
|
||||
// 登陆验证
|
||||
guard let username = info ["username"] else {
|
||||
print("请输入用户名")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// 隐式解包的定义
|
||||
// 在类型的后边加上感叹号 !
|
||||
let num1: Int! = 10
|
||||
let num2: Int = num1
|
||||
|
||||
|
||||
// 字符串插值的几种方式
|
||||
var num3: Int? = 20
|
||||
// 1. 强制解包
|
||||
print("num3 is \(num3!)")
|
||||
// 2. 字符串的方法 String(describing: num3)
|
||||
print("num3 is \(String(describing: num3))")
|
||||
// 3. ?? 空合并运算符
|
||||
print("num3 is \(num3 ?? 0)")
|
||||
|
||||
|
||||
//: 多重可选项
|
||||
/*:
|
||||
通过lldb指令 frame variable -R 或者 fr v -R 察看区别
|
||||
必须是项目而非playground
|
||||
*/
|
||||
var num1: Int? = 10
|
||||
var num2: Int?? = num1
|
||||
var num3: Int?? = 10
|
||||
|
||||
print(num2 == num3)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//: [Next](@next)
|
@@ -5,5 +5,7 @@
|
||||
<page name='02 数据类型'/>
|
||||
<page name='03 流程控制'/>
|
||||
<page name='04 函数'/>
|
||||
<page name='05 枚举'/>
|
||||
<page name='06 可选项'/>
|
||||
</pages>
|
||||
</playground>
|
Binary file not shown.
Reference in New Issue
Block a user