枚举以及可选项

This commit is contained in:
2021-12-05 21:18:35 +08:00
parent 725918c297
commit 8af44f0613
4 changed files with 322 additions and 0 deletions

View File

@@ -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) //
//: #####
/*:
IntStringSwift
- Int0
- String
*/
//: #### MemoryLayout
/*:
MemoryLayoutsizestridealignment
- 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.
- alignmentThe 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. Puppy8Int
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. AlternatePuppy8Int
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)

View File

@@ -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)truefalse
*/
if let number = Int( "123") {
print("字符串转换整数成功:\(number) ")
// numberInt
// 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")
}
// ,
// firstsecondfirst < 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
- ba
a ?? b
- anila
- anilb
- ba
*/
let a: Int? = 1
let b: Int? = 2
let c = a ?? b // cInt? Optional(1)
let a: Int? = nil
let b: Int? = 2
let c = a ?? b // cInt Optional(2)
let a: Int? = 1
let b: Int = 2
let c = a ?? b // cInt, 2
// ?? if使
let a: Int? = nil
let b: Int? = 2
if let c = a ?? b {
print(c)
}
//: #### guard
/*:
guard else {
// do something
退
// returnbreakthrow
}
- guardfalse
- guardtrueguard
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)

View File

@@ -5,5 +5,7 @@
<page name='02 数据类型'/>
<page name='03 流程控制'/>
<page name='04 函数'/>
<page name='05 枚举'/>
<page name='06 可选项'/>
</pages>
</playground>