-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01Basics.swift
More file actions
141 lines (109 loc) · 4.43 KB
/
01Basics.swift
File metadata and controls
141 lines (109 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// 01Basics.swift
// SwiftTutorial
//
// Created by deerdev on 2017/1/30.
// Copyright © 2017年 deerdev. All rights reserved.
//
import Foundation
func basicDefination() -> Void {
var type: String = String()
var red = 1, green = "aa", blue = 0.0
var str = "swift"
/// 如果使用关键字作为变量。添加反引号(`x`)
var `var` = "var"
print(red)
// print默认换行,加terminator不换行
print(green, terminator:"")
print(blue, terminator:" ")
print(`var`)
/// 格式化输出字符串(字符串插值)
print("I'm learing \(str)")
/// 输出UInt8的最大最小值,适用于UInt16、UInt32等
let minValue = UInt8.min
let maxValue = UInt8.max
print("UInt8:min = \(minValue); max = \(maxValue)")
/// 区分数值"_",增加可读性
let x = 1_000_000_000
print("Beautiful reading: \(x)")
/// 类型别名
typealias AudioSample = UInt16
var maxAudioSample = UInt16.max
print("macAudio : \(maxAudioSample)")
/// if的判断条件只能是Bool型,Int不行(直接报错)
/*
if maxAudioSample {
}
*/
/// 元组
let (code, message1) = ("code", "message")
// 只取第二个变量
let (_, message2) = ("code", "message")
let codeStatus = (404, "error")
let codeStatus2 = (statusCode:404, statusMessage:"error")
print("Tuple1: \(codeStatus.0) - \(codeStatus.1)")
print("Tuple2: \(codeStatus2.statusCode) - \(codeStatus2.statusMessage)")
// 当【比较】两个Tuple类型的变量时,要遵循下面的规则:
// -**- 首先,只有元素个数相同的Tuple变量之间,才能进行比较
// -**- 最多包含6个元素的Tuple变量进行比较,超过这个数量,Swift会报错
let possibleNumber = "123"
/// convertedNumber是optional类型,因为返回Int()强制转换可能失败
let convertedNumber = Int(possibleNumber)
/// optional初始化,可不初始化,为nil
/********
*swift中的nil不是指针,是确定的值,任何值得可选状态都可以被置为nil
*OC中的nil是指向一个不存在的对象
********/
var error404: Int? = 404
if convertedNumber != nil {
print("操作 convertedNumber")
}
// *** let使用前必须初始化,没有默认初始化
let optionNum1:Int?
// *** var变量默认初始化为nil
var optionNum2:Int?
/// 确定convertedNumber有值(必须非nil),使用"!"强制解析optional(转为非optional)
var number = convertedNumber!
/// 可选绑定(optional binding)
if let constNumber = Int(possibleNumber) {
print("optional binding test")
}
/// 隐式解析可选类型
// 使用!,而不是?,适用于:赋值一次,以后都会有值的optional
// 在使用前,一定是已经完成初始化了
// assumedString: 隐式可选类型
let assumedString: String! = "xxx"
// 不用使用!解析,直接赋值使用
let implicitString: String = assumedString
let implicitString2 = assumedString
/// 错误处理
/*
func makeASandwich() throws {
// ...
}
do {
try makeASandwich()
eatASandwich()
} catch SandwichError.outOfCleanDishes {
washDishes()
} catch SandwichError.missingIngredients(let ingredients) {
// 处理带错误参数
buyGroceries(ingredients)
}
*/
/// 断言assertion
let age = 3, indexx = 0
// 条件为真,继续运行;
// 条件为假,停止运行,打印第二个参数
assert(age >= 0, "A person's age can't be less than zero")
assert(age >= 0)
// 直接断言失败 `assertionFailure(_:file:line:)`
assertionFailure("A person's age can't be less than zero.")
// 先决判断: 使用全局 precondition(_:_:file:line:) 函数来写一个先决条件, 当表达式的结果为 false 的时候这条信息会被显示(显式信息不报错)
precondition(indexx > 0, "Index must be greater than zero.")
// `preconditionFailure(_:file:line:)` 方法来表明出现了一个错误,例如,switch 进入了 default 分支,但是所有的有效值应该被任意一个其他分支(非 default 分支)处理。
/// ==========Swift4.2==========
/// Bool .toggle(), 给Bool取反
var aa = false
aa.toggle() // True
}