补充一下Swift的相关知识。

关于数组的一些操作

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
import Cocoa

//创建一个数组的几种方式
var a = [1,2,3]

var b: [Int] = [3,4,5]

var c: Array<Int> = [6,7,8]

var d = Array(repeating: -1, count: 3) //利用Array初始化器生成值为-1,数量为3的数组
print(d)
/*
[-1, -1, -1]
*/

//数组的添加
d += [7,8,9] //可以直接用加号连接两个数组
d = d + [3]
print(d)

/*
[-1, -1, -1, 7, 8, 9, 3]
*/

d.append(10)
print(d)

/*
[-1, -1, -1, 7, 8, 9, 3, 10]
*/

d.insert(9, at: 0) //根据索引插入
print(d)

/*
[9, -1, -1, -1, 7, 8, 9, 3, 10]
*/

//数组的更改
d[0] = 100

d.replaceSubrange(0..<2, with: [2])
print(d)

/*
[2, -1, -1, 7, 8, 9, 3, 10]
*/

//数组的查找
print(d.contains(9))

/*
true
*/

//数组的删除
d = [2, -1, -1, 7, 8, 9, 3, 10] //初始化

print(d.remove(at: 0))
print(d)

/*
[-1, -1, 7, 8, 9, 3, 10]
*/

//数组倒序排序
var f = ["A", "B", "K", "D", "E"] //初始化
f.sort(by: {(s1, s2) -> Bool in
if s1 > s2 {
return true
}else {
return false
}
})
print(f)

//数组过滤
f = ["A", "B", "K", "D", "E"] //初始化

let g = f.filter({(s0) -> Bool in
if s0 != "K" {
return true
}else {
return false
}
})
print(g)

//遍历数组
f = ["A", "B", "K", "D", "E"] //初始化
for item in f {
print(item)
}

for item in 0..<f.count {
print(f[item])
}

for item in f[0...] {
print(item)
}

/*
A
B
K
D
E
*/

for item in (f[1...3]).reversed() {
print(item)
}

/*
D
K
B
*/

关于集合的一些操作

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
import Cocoa

//创建一个集合
var a: Set = [1,2,3,4]

var b: Set<String> = ["Hello", "World"]

var c: Set<Int> = []

//元素数量
a = [1,2,3,4] //初始化
print(a.count)

/*
4
*/

//添加一个元素
a = [1,2,3,4] //初始化
a.insert(6)
print(a)

/*
[4, 1, 2, 3, 6]
*/

//查找值是否存在
a = [1,2,3,4] //初始化
print(a.contains(999))

/*
false
*/

//删除某个值
a = [1,2,3,4] //初始化
a.remove(4)
print(a)

/*
[1, 2, 3]
*/

//两个集合合并
a = [1,2,3,4]
var e: Set<Int> = [999,1000,1200,1]
var f = a.union(e)
print(f)

/*
[1200, 2, 999, 4, 1000, 3, 1]
*/

//返回两个集合相同的数据
a = [1,2,3,4]
e = [999,1000,1200,1]
var g = a.intersection(e)
print(g)

/*
[1]
*/

//返回两个集合不同的数据
a = [1,2,3,4]
e = [999,1000,1200,1]
var h = a.symmetricDifference(e)
print(h)

/*
[4, 2, 3, 1200, 999, 1000]
*/

//两个集合比较,返回前面集合与后面集合不同的元素
a = [1,2,3,4]
e = [999,1000,1200,1]
var i = a.subtracting(e)
print(i)

/*
[2,3,4]
*/

//集合过滤
a = [1,2,3,4]
var k = a.filter({(th1) -> Bool in
if th1 == 3 {
return false
}else {
return true
}
})
print(k)

/*
[1, 4, 2]
*/

关于字典的一些操作

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
import Cocoa

//创建字典的方式
var a = [1: "A", 2: "B", 3: "C"]

var b: Dictionary<Int, String> = [1: "A", 2: "B"]

var c: [Int: String] = [1: "A", 2: "B"]

var d = [Int: String]() //创建空字典

//字典的取值
var e = ["hello": "A", "world": "B"]
print(e["hello"] ?? "unknown")

/*
A
*/

//修改键值对
e = ["hello": "A", "world": "B"]
e["hello"] = "ASDFASDF"
print(e)

/*
["world": "B", "hello": "ASDFASDF"]
*/

e.updateValue("QWEQWE", forKey: "hello") //如果有这个key则修改,没有这个key则会创建
e.updateValue("IOPIOP", forKey: "swift")
print(e)

/*
["world": "B", "swift": "IOPIOP", "hello": "QWEQWE"]
*/

//删除
e = ["hello": "A", "world": "B"]
e.removeValue(forKey: "hello")
print(e)

/*
["world": "B"]
*/

//过滤
e = ["hello": "A", "world": "B", "Swift": "C", "iOS": "D"]
let k = e.filter({(key, value) -> Bool in
if key == "iOS" {
return false
}
return true
})
print(k)

/*
["world": "B", "hello": "A", "Swift": "C"]
*/

//遍历
e = ["hello": "A", "world": "B", "Swift": "C", "iOS": "D"]
for (a,b) in e { //字典转换为元组遍历
print("key = " + a + " Value = " + b)
}

/*
key = Swift Value = C
key = world Value = B
key = hello Value = A
key = iOS Value = D
*/

关于函数的一些操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import Cocoa

//可变参数
func test(name1: String...){
for item in name1 {
print(item)
}
}
test(name1: "1","2","3")

//assert断言
func test2(outname inname: String){
if inname == "hello" {
assert(false, "停止运行")
}
print(inname)
}
test2(outname: "hello")

guard拦截语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//简单拦截
func test(_ inname: Int){
guard inname > 10 else {
print("进入guard")
return
}
print("不拦截")
}
test(1)

//可变参数为空时拦截
var a:String? = "hello"
func test2(_ inname: String?){
guard let value = inname else {
print("该值无法获取")
return
}
print(value)
}
test2(a)

inout 值传递

将函数导入的值变为变量

1
2
3
4
5
6
7
8
9
10
11
12
func double(num: inout Int) {
num = num * 2
}
var a = 12
print(a)
double(num: &a)
print(a)

/*
12
24
*/

函数类型和匿名函数

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
import Cocoa

//创建函数类型范例
var a: () -> Void

var b: (String, Int) -> String

var c: ([Int]) -> (Int, String)

//函数类型使用方式
func pt() -> Void {
print("这是一个函数")
}
var d: () -> Void = pt
d()

//使用匿名函数的方式来使用
var e: () -> Void = { () -> Void in
print("这是一个函数")
}
e()

/*
这是一个函数
*/

var f: (String, Int) -> String = {(name: String, age: Int) -> String in
return "His name is \(name) .\nAge: \(age)"
}
print(f("Bob", 12))

/*
His name is Bob .
Age: 12
*/

var g: ([Int]) -> String = {(numbers: Array<Int>) -> String in
var txt = ""
for item in numbers {
txt += String(item) + " "
}
return txt
}
print(g([1,2,3,4]))

/*
1 2 3 4
*/

//匿名函数作为参数
func sum(add: (Int, Int) -> Int){
let h = add(1,2)
print(h)
}
sum(add: {(number1: Int, number2: Int) -> Int in
return number1 + number2
})

函数作为返回值

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
import Cocoa

func a (num: Int) -> Int{
return num * num
}

func b (num: Int) -> Int{
return num + num
}
func k(choose: Bool) -> (Int) -> Int {
return choose ? a : b
}

var c = k(choose: true)
print(c(10))

/*
100
*/

//函数也可以内嵌
func h(choose: Bool) -> (Int) -> Int {
func p (num: Int) -> Int{
return num * num
}

func qewan@foxmail.com (num: Int) -> Int{
return num + num
}

return choose ? p : qewan@foxmail.com
}

var j = h(choose: true)
print(j(10))

/*
100
*/

函数作为参数

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
func a (num: Int) -> Int{
return num * num
}

func b (num: Int) -> Int{
return num + num
}
func k(choose: Bool) -> (Int) -> Int {
return choose ? a : b
}

var c = k(choose: true)
print(c(10))

/*
100
*/

//函数也可以内嵌
func h(choose: Bool) -> (Int) -> Int {
func p (num: Int) -> Int{
return num * num
}

func qewan@foxmail.com (num: Int) -> Int{
return num + num
}

return choose ? p : qewan@foxmail.com
}

var j = h(choose: true)
print(j(10))

/*
100
*/

隐式函数及简写

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
import Cocoa

//完整案例
var a: () -> Void = {() -> Void in
print("这是一个隐式函数")
}

a()

/*
这是一个隐式函数
*/

//简写范例
var b:() -> Void = {
print("当没有参数和返回值时,这是一个隐式函数的简写")
}
b()

/*
当没有参数和返回值时,这是一个隐式函数的简写
*/

var c = {}
print(type(of: c)) //根据类型推断来推断这是一个没有参数和返回值的函数

/*
() -> ()
*/

//简写范例
func d(param: () -> Void) {
param()
}

d(param: {() -> Void in //完整方式
print("hello")
})
d(param: {
print("hello")
})

/*
hello
hello
*/

//可以使用$和索引来代表第几个参数
func test(param: (Int, Int) -> Int){
print(param(12,3))
}
test(param: {return $0 + $1})
test(param: {$0 - $1})

/*
15
9
*/

关于枚举的一些操作

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
 import Cocoa


//创建枚举
enum Number{
case A
case B
case C
}

func test(num: Number){
if num == Number.A {
print("a")
}else if num == Number.B {
print("b")
}else {
print("c")
}
}
test(num: Number.B)

/*
b
*/

//其他创建方式
enum Jump {
case A,B,C
}

print(Jump.B)

/*
B
*/

//设置枚举初始值
enum Pop: String {
case A = "Hello"
case B = "World"
}
print(Pop.A.rawValue)

/*
Hello
*/

//枚举相关值
enum People {
case name(String)
case age(Int)
case tall(Double)
}

func test(msg: People){
switch msg {
case People.age(18):
print("hello 18")
case People.name("Bob"):
print("Bob")
case People.tall(188.1):
print("That's right")
default:
print("没有匹配")
}
}
test(msg: People.age(18))

/*
hello 18
*/

//遍历枚举,生成枚举数组
enum Word: CaseIterable {
case A,B,C,D
}
print(type(of: Word.allCases))
for i in Word.allCases {
print(i)
}

for index in 0..<Word.allCases.count {
print(Word.allCases[index])
}

/*
A
B
C
D
A
B
C
D
*/

关于结构的一些操作

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
import Cocoa

struct Student {
var name = "unknown"
var age = 0
var score: Double = 0
var isPass = false
static var schoolName = "家里蹲大学"

func passOrNot(score: Double) -> Bool {
if score >= 60 {
return true
}else {
return false
}
}

init(name: String, age: Int, score: Double) { //初始化
self.name = name
self.age = age
self.score = score
self.isPass = passOrNot(score: score)
}

func getName() -> String {
return name
}

func getAge() -> Int {
return age
}

func getScore() -> Double {
return score
}

func getIsPass() -> Bool {
return isPass
}

mutating func setScore(score: Double) { //改变结构体变量用mutating
self.score = score
self.isPass = passOrNot(score: score)
}
}

var a = Student(name: "小明", age: 18, score: 91.5)
print("姓名:" + a.name)
print("年龄:" + String(a.age))
print("分数:" + String(a.score))
print("是否及格:" + (a.isPass ? (a.score == 100 ? "完美" : "及格") : "不及格"))
print("学校:" + Student.schoolName)

/*
姓名:小明
年龄:18
分数:91.5
是否及格:及格
学校:家里蹲大学
*/

a.setScore(score: 10.5)
print("分数:" + String(a.score))
print("是否及格:" + (a.isPass ? (a.score == 100 ? "完美" : "及格") : "不及格"))

/*
分数:10.5
是否及格:不及格
*/

//结构是值传递,类是传递引用
struct Test {
var score = 10
}

var y = Test()
print(y.score)

var z = y
z.score = 100
print(y.score)

/*
10
10
*/

计算属性

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
import Cocoa

struct Person {
private var value = String()

var name: String{
set {
print("运行set" + newValue) //在不设置变量的时候默认为newValue //不设置set时该变量为只读属性
}

// set(param){
// print("运行set" + param)
// }

get {
print("运行get")
return value
}
}

init() {

}
}

var a = Person()
a.name = "hello"

/*
运行get
*/

a.name

/*
运行set
*/

参考资料

Swift编程基础