143 lines
4.1 KiB
Swift
143 lines
4.1 KiB
Swift
import UIKit
|
||
|
||
var greeting = "Hello, playground"
|
||
|
||
//: [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)
|