基础部分

常量与变量

  • let(常量)
  • var(变量)
1
2
3
var x = 0.0, y = 0.0, z = 0.0 // 一行声明多个变量
var msg: String // 标注类型
println("my name is \(name)") // 字符串插值

注释

  • 单行注释
  • 多行注释
1
2
3
// 这是一个注释
/* 这是一个
多行注释 */

数据类型

  • 整型
    • Int(有符号,32位平台为Int32,64位平台为Int64)
      • Int8
      • Int16
      • Int32
      • Int64
    • UInt(无符号,32位平台为UInt32,64位平台为UInt64)
      • UInt8
      • UInt16
      • UInt32
      • UInt64
  • 浮点型
    • Double(64位,15位数字)
    • Float(32位,6位数字)
  • 布尔型
    • true
    • false
  • 元组
  • nil

整数

1
2
let minValue = UInt8.min // minValue为0
let maxValue = UInt8.max // maxValue为255

浮点型

1
let pi = 3.14159 // pi会被推测为Double类型

元组

1
2
3
4
5
6
7
8
9
10
11
12
13
let http404Error = (404, "Not Found")
let (statusCode, statusMessage) = http404Error
println(statusCode") // 404
println(statusMessage) // Not Found
// 只需一部分,用下划线(_)代替
let (justTheStatusCode, _) = http404Error
// 访问单个元素
println(http404Error.0) // 404
println(http404Error.1) // Not Found
// 给单个元素命名
let http200Status = (statusCode: 200, description: "OK")
println(http200Status.statusCode) // 200
println(http200Status.description) // OK

数值型字面量

  • 十进制(没有前缀)
  • 二进制(0b)
  • 八进制(0o)
  • 十六进制(0x)
1
2
3
4
5
1.25e2 // 表示1.25x10^2
1.25e-2 // 表示1.25x10^-2
0xFp2 // 表示15x2^2
0xFp-2 // 表示15x2^-2
lef oneMillion = 1_000_000 // 整数和浮点数都可以加_来增强可读性

数值类型转化

1
2
3
4
5
6
7
8
9
// 整型转化
let one: UInt8 = 1
let two: UInt16 = UInt16(one)
// 整型转浮点型
lef i = 3
let d = Double(i)
// 浮点型转整型
let pi = 3.14
let three = Int(pi) // 浮点值会被截断

类型别名

1
2
typealias AudioSample = UInt16
var min = AudioSample.min // min为0

可选类型

1
2
3
4
5
6
7
8
9
// option为可选类型
if let const = option {
// code
}
// 可选(?)和隐式可选(!)的区别
let h: String? = "hello"
println(h!)
let w: String! = "world"
print(w)

调试

1
2
let age = -3
assert(age >= 0, "cannot be less than zero") // 条件为false程序终止

基本运算符

  • 赋值运算(=)
  • 算数运算
    • 加法(+)
    • 减法(-)
    • 乘法(*)
    • 除法(/)
    • 求余(%)
  • 自增和自减运算
    • 自增(++)
    • 自减(—)
  • 复合赋值(+=)
  • 比较运算
    • 等于(==)
    • 不等(!=)
    • 大于(>)
    • 小于(<)
    • 大于等于(>=)
    • 小于等于(<=)
  • 三目运算(bool?a:b)
  • 空合运算(a??b)
  • 区间运算
    • 闭区间(…)
    • 半开区间(..<)
  • 逻辑运算
    • 逻辑非(!a)
    • 逻辑与(a&&b)
    • 逻辑或(a||b)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 赋值
let (x, y) = (1, 2)
// 求余
8 % 2.5 // 等于0.5
// 空合运算
var b = nil
var c = "not nil"
var a = b ?? c // not nil
// 闭区间
for index in 1...5 {
println(index) // index为1~5
}
// 半开区间
for index in 0..<4 {
print(index) // index为0~3
}

字符串和字符

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
// 字面量
let dollarSign: Character = "\u{24}"
// 初始化空字符串
var empty = ""
var anotherEmpty = String()
// 判断是否为空
if empty.isEmpty {
println("empty")
}
// 遍历字符串
for character in "Dog" {
print(character)
}
// 字符数
let str = "hello"
println(countElements(str)) // 5
// 字符串插值
let greeting = "hello"
let msg = "\(greeting) world"
// 字符串相等
let a = "hello world"
let b = "hello world"
if a == b {
println("the same")
}
// 前缀后缀
let str = "prefix and suffix"
println(str.hasPrefix("prefix")) // true
println(str.hasSuffix("suffix")) // true
// 大写小写
let str = "hello world"
let upperCase = str.uppercaseString // HELLO WORLD
let lowerCase = upperCase.lowercaseString // hello world
// Unicode
let dogString = "Dog!🐶"
for codeUnit in dogString.utf8 {
print("\(codeUnit) ")
}
// 68 111 103 33 240 159 144 182
for codeUnit in dogString.utf16 {
print("\(codeUnit) ")
}
// 68 111 103 33 55357 56374
for scalar in dogString.unicodeScalars {
print("\(scalar.value) ")
}
// 68 111 103 33 128054

集合类型

  • 数组
  • 字典

数组

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
// 构造数组
var a = []
var b = [Int]()
// 构造3个同为0.0的初始值
var c = [Double](count: 3, repeatedValue: 0.0)
// 数组相加
var d = ["h", "e"] + ["l", "l", "o"]
var shoppingList: [String] = ["Eggs", "Milk"]
// 数组大小
shoppingList.count
// 是否为空
if shoppingList.isEmpty {
println("list is empty")
}
// 添加一项到最后
shoppingList.append("Flour")
// 添加多项到最后
shoppingList += ["Cheese", "Butter"]
// 添加一项到指定位置
shoppingList.insert("Bean", atIndex: 0)
// 删除指定一项
shoppingList.removeAtIndex(0)
// 删除最后一项
shoppingList.removeLast()
// 批量替换
shoppingList[1...3] = ["Bananas", "Apples"]
// 遍历
for item in shoppingList {
println(item)
}
for (index, value) in enumerate(shoppingList) {
println("Item \(index + 1): \(value)")
}

字典

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
// 构造字典
var a = ["TYO": "Tokyo", "DUB": "Dublin"]
var b = Dictionary<Int, String>()
var c[16] = "sixteen"
var c = [:]
var airports: [String: String] = ["TYO": "Tokyo", "DUB": "Dublin"]
// 字典大小
airports.count
// 是否为空
if airports.isEmpty {
println("list is empty")
}
// 添加一项
airports["LHR"] = "London"
// 修改
airports["LHR"] = "London Heathrow"
// 修改,返回修改前的值
if let oldValue = airports.updateValue("Dublin Internation", forKey: "DUB") {
println("The old vaule was \(oldValue)")
} else {
println("That is not in the list")
}
// 删除
airports["LHR"] = nil
// 删除,返回被删除的值
if let removedValue = airports.removeVauleForKey("DUB") {
println("The removed item is \(removedValue)")
} else {
println("That is not in the list")
}
// 遍历
for (airportCode, airportName) in airports {
println("\(airportCode): \(airportName)")
}
// 遍历key
for airportCode in airports.keys {
println(airportCode)
}
// 遍历value
for airportName in airports.values {
println(airportName)
}
// 用keys或values构造新数组
let airportCodes = Array(airports.keys)
let airportNames = Array(airports.values)

控制流

  • 循环语句
    • for
    • while
  • 条件语句
    • if
    • switch
    • where
  • 控制转移语句
    • continue
    • break
    • fallthrough
    • return

循环语句

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
// 遍历区间
for _ in 1...5 {
println("hello world");
}
// 遍历数组
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
println("Hello, \(name)")
}
// 遍历字典
let numOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (name, count) in numOfLegs {
println("\(name) have \(count) legs")
}
// 遍历字符串
for char in "Hello" {
println(char)
}
// 条件递增
for var index = 0; index < 3; ++index {
println(index)
}
// while循环
var i = 0
while i < 10 {
println(i++)
}
var j = 0
do {
println(j++)
} while j < 10

条件语句

if

1
2
3
4
5
6
7
8
var a = -1
if a < 0 {
println("condition 1")
} else if (a > 10) {
println("condition 2")
} else {
println("condition 4")
}

switch

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
// 正确
let someCharacter: Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
println("\(someCharacter) is a vowel")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
println("\(someCharacter) is a consonant")
default:
println("\(someCharacter) is not a vowel or a consonant")
}
// 错误
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a":
case "A":
println("The letter A")
default:
println("Not the letter A")
}
// 区间匹配
let score = 80
switch score {
case 0...59:
println("bad")
case 60...79:
println("ok")
case 80...89:
println("excellent")
case 90...100
println("perfect")
}
// 元组
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
println("(0, 0) is at the origin")
case (_, 0):
println("(\(somePoint.0), 0) is on the x-axis")
case (0, _):
println("(0, \(somePoint.1)) is on the y-axis")
case (-2...2, -2...2):
println("(\(somePoint.0), \(somePoint.1)) is inside the box")
default:
println("(\(somePoint.0), \(somePoint.1)) is outside of the box")
}
// 值绑定
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x, 0):
println("on the x-axis with an x value of \(x)")
case (0, let y):
println("on the y-axis with a y value of \(y)")
case let (x, y):
println("somewhere else at (\(x), \(y))")
}
// where
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
println("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
println("(\(x), \(y)) is on the line x == -y")
case let (x, y):
println("(\(x), \(y)) is just some arbitrary point")
}

控制转移语句

continue

1
2
3
4
5
6
7
8
9
10
11
12
// 继续下个循环
let input = "great minds think alike"
var output = ""
for char in input {
switch char {
case "a", "e", "i", "o", "u":
continue
default:
output.append(char)
}
}
println(output)

break

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 循环语句中的break,结束循环
for char in "hello" {
if char == "l" {
break
}
}
// switch语句中得break,忽略该分支
let numberSymbol: Character = "三"
switch numberSymbol {
case "一":
println("1")
case "二":
println("2")
case "三":
println("3")
case "四":
println("4")
default:
break
}

fallthrough

1
2
3
4
5
6
7
8
9
10
11
// 使用fallthrough贯穿下个分支
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and also"
fallthrough
default:
description += " an integer."
}
println(description)

带标签的语句

1
2
3
4
5
6
7
8
// 在使用breakcontinue的时候可以指定操作哪个控制流
var i = 0;
loop: while i < 100 {
if i == 30 {
break loop
}
i++
}

函数

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
// 有返回值
func len(start: Int, end: Int) -> Int {
return end - start
}
// 无返回值
func sayHello(name: String) {
println("Hello, \(name)")
}
// 多重返回值
func search(name: String) -> (id: Int, name: String) {
var map = ["jason": 1, "mark": 2]
return (name, map[name])
}
// 外部参数名
func join(str s1: String, toStr s2: String, joiner joiner: String) -> String {
return s1 + joiner + s2
}
join(str: "hello", toStr: "world", joiner: ",")
// 简写外部参数名
func join(#str: String, #toStr: String, #joiner: String) -> String {
return str + joiner + toStr
}
join(str: "hello", toStr: "world", joiner: ",")
// 默认参数
func join(str s1: String, toStr s2: String, joiner joiner: String = " ") -> String {
return str + joiner + toStr
}
join(str: "hello", toStr: "world")
// 默认带上外部参数
func join(s1: String, s2: String, joiner: String = " ") -> String {
return s1 + joiner + s2
}
join("hello", "world", joiner: "-")
// 可变参数
func arithmeticMean(numbers: Double...) -> Double {
var total: Double = 0
for number in numbers {
total += number
}
return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
// 函数参数默认是常量,不能改变,可以加var来定义变量参数
func hello(var str: String) -> String {
str += ",hello"
return str
}
// 改变参数值,加inout,且不能被var或let标记
func swapTwoInts(inout a: Int, inout b: Int) {
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt) // 传入时加&
println("\(someInt) and \(anotherInt)") // 107 and 3
// 使用函数
var mathFunction: (Int, Int) -> Int = addTwoInts
var anotherMathFunction = addTwoInts
// 函数作为参数
func printMathResult(mathFunction: (Int, Int) -> Int, a: Int, b: Int) {
println("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5)
// 函数作为返回值
func stepForward(input: Int) -> Int {
return input + 1
}
func stepBackward(input: Int) -> Int {
return input - 1
}
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
return backwards ? stepBackward : stepForward
}
// 嵌套函数
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
func stepForward(input: Int) -> Int { return input + 1 }
func stepBackward(input: Int) -> Int { return input - 1 }
return backwards ? stepBackward : stepForward
}

闭包

格式

1
2
3
4
// 格式
{ (parameters) -> returnType in
statements
}

闭包作为参数

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
// 原式
func backwards(s1: String, s2: String) -> Bool {
return s1 > s2
}
var reversed = sorted(names, backwards)
// 使用闭包
var reversed = sorted(names, { (s1: String, s2: String) -> Bool in
return s1 > s2
})
// 根据上下文推断类型
var reversed = sorted(names, { s1, s2 in return s1 > s2 })
// 单表达式闭包隐式返回
var reversed = sorted(names, { s1, s2 in s1 > s2})
// 参数名称缩写
var reversed = sorted(names, { $0 > $1 })
// 运算符函数
var reversed = sorted(names, >)
// 尾随闭包进行函数调用
func someFunctionThatTakesAClosure(closure: () -> ()) {
// 函数体部分
}
someFunctionThatTakesAClosure() {
// 闭包主体部分
}
// 尾随闭包进行函数调用(例子)
var reversed = sorted(names) { $0 > $1 }

闭包作为返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
// 闭包是引用类型,将闭包赋给两个不同的变量/常量,两个值都指向一个闭包
func makeIncrementor(forIncrement amount: Int) -> () -> Int {
var runningTotal = 0
func incrementor() -> Int {
runningTotal += amount
return runningTotal
}
return incrementor
}
let incrementByTen = makeIncrementor(forIncrement: 10)
let incrementBySeven = makeIncrementor(forIncrement: 7)
incrementBySeven() // 返回的值为7
incrementByTen() // 返回的值为40

枚举

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
enum CompassPoint {
case North
case South
case East
case West
}
// 一行
enum Planet {
case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}
var directionToHead = CompassPoint.West
directionToHead = .East // 前面已经赋值了,可以用忽略前缀
switch directionToHead {
case .North:
println("North")
case .South:
println("South")
case .East:
println("East")
case .West:
println("West")
}
// 使用案例
enum Barcode {
case UPCA(Int, Int, Int)
case QRCode(String)
}
var productBarcode = Barcode.UPCS(8, 85909_51226, 3)
productBarcode = .QRCode("ABCDEFGHIJKLMNOP")
// 用法
switch productBarcode {
case .UPCA(let numberSystem, let identifier, let check):
println("UPC-A with value of \(numberSystem), \(identifier), \(check).")
case .QRCode(let productCode):
println("QR code with value of \(productCode).")
}
// 简化
switch productBarcode {
case let .UPCA(numberSystem, identifier, check):
println("UPC-A with value of \(numberSystem), \(identifier), \(check).")
case let .QRCode(productCode):
println("QR code with value of \(productCode).")
}
// 赋原始值,当整型被赋原始值,其他成员没有值,会自动递增
enum Planet: Int {
case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}
let earthsOrder = Planet.Earth.toRaw() // earthsOrder为3
let possiblePlanet = Planet.fromRaw(7) // possiblePlanet为可选值,为Planet.Uranus

未完待续

文章目录
  1. 1. 基础部分
    1. 1.1. 常量与变量
    2. 1.2. 注释
    3. 1.3. 数据类型
      1. 1.3.1. 整数
      2. 1.3.2. 浮点型
      3. 1.3.3. 元组
      4. 1.3.4. 数值型字面量
      5. 1.3.5. 数值类型转化
      6. 1.3.6. 类型别名
      7. 1.3.7. 可选类型
    4. 1.4. 调试
  2. 2. 基本运算符
  3. 3. 字符串和字符
  4. 4. 集合类型
    1. 4.1. 数组
    2. 4.2. 字典
  5. 5. 控制流
    1. 5.1. 循环语句
    2. 5.2. 条件语句
      1. 5.2.1. if
      2. 5.2.2. switch
    3. 5.3. 控制转移语句
      1. 5.3.1. continue
      2. 5.3.2. break
      3. 5.3.3. fallthrough
      4. 5.3.4. 带标签的语句
  6. 6. 函数
  7. 7. 闭包
    1. 7.1. 格式
    2. 7.2. 闭包作为参数
    3. 7.3. 闭包作为返回值
  8. 8. 枚举