Switch入门第二讲
1 //元组
2 let student = (18, 70.0, "王恒")
3
4 let student1: (Int,Double,String) = (22, 65.0, "老王")
5 print(student1)
6
7 let 老张 = (age: 24, height: 169.0, sex: "泰国回来的,未知")
8 print(老张.sex)
9
10 let 同桌 = (age: 38, height: 150.0, sex: "母")
11 print(同桌.age)
12
13 //数组
14 var array: Array = [1, 2, 3, 4, 5]
15 var array1: Array = [1, "2", "sdg"]
16 //定义一个int类型的数组
17 var intArray: Array<Int> = [1, 2, 3, 4]
18 var intArray1: [Int] = [1, 2, 3]
19 //定义字符串类型数组
20 var stringArray: [String] = ["张三", "李四", "王五"]
21 //定义一个Int类型的空数组
22 var intArray2 = [Int]()
23
24 // 数组的增删改查
25 //增
26 var arrayValue = [1, 2, 3, 4, 5]
27 arrayValue += arrayValue[0...2]
28 print(arrayValue)
29
30 //添加一个值到数组的最后一位
31 arrayValue.append(100)
32 print(arrayValue)
33
34 var arrayValue1 = [10, 11, 12]
35 //在数组的最后添加一个数组
36 arrayValue.appendContentsOf(arrayValue1)
37 print(arrayValue)
38
39 //插入一个数据到某个位置
40 arrayValue.insert(101, atIndex: 0)
41 print(arrayValue)
42
43 //把一个数组插入到某一个位置
44 arrayValue.insertContentsOf(arrayValue1, at: 0)
45 print(arrayValue)
46
47
48
49 //删
50 //删除数组的第一个元素
51 arrayValue.removeFirst()
52 print(arrayValue)
53
54 //删除最后一个元素
55 arrayValue.removeLast()
56 print(arrayValue)
57
58 //根据下标删除
59 arrayValue.removeAtIndex(2)
60 print(arrayValue)
61
62 //删除一个区间
63 arrayValue.removeRange(0...2)
64 print(arrayValue)
65
66 //删除所有的元素
67 arrayValue.removeAll()
68 print(arrayValue)
69
70 //删除完数据后保不保存存储空间 true:保存 false:不保存
71 arrayValue.removeAll(keepCapacity: true)
72 print(arrayValue.capacity)
73
74
75 //字典
76 let dicValue: Dictionary = [1 : "1", "2" : 2]
77
78 let dicValue1: Dictionary<String, String> = ["1" : "1", "2" : "2"]
79
80 var dicValue2: Dictionary<String, String> = ["apple":"🍎", "cat":"🐱","dog":"🐶", "pig":"🐷", "zhangyu":"🐙"]
81 //如果字典里有对应的key,则修改其值,如果没有,则添加该键值对
82 dicValue2.updateValue("梨", forKey: "apple")
83 print(dicValue2)
84 dicValue2.updateValue("🍌", forKey: "banana")
85 print(dicValue2)
86 //和上面效果一样
87 dicValue2["apple"] = "🍎"
88 dicValue2["sheep"] = "🐑"
89 print(dicValue2)
90
91 //删除
92 dicValue2.removeValueForKey("sheep")
93 print(dicValue2)
94 //删除全部
95 //dicValue2.removeAll()
96 //print(dicValue2)
97
98 //遍历所有的key
99 for key in dicValue2.keys
100 {
101 print(key)
102 }
103
104 //遍历所有的value
105 for var value in dicValue2.values
106 {
107 print(value)
108 }
109
110 //遍历键值对
111 for (key, value) in dicValue2
112 {
113 print("\(key) -> \(value)")
114 }
115
116 //函数
117 /*
118 * 函数书写格式
119 * func 函数名 (参数名: 参数类型,参数名: 参数类型...) -> 返回值类型
120 {
121 函数体
122 }
123 */
124
125 // 无参无返回值类型函数
126 func sayHello()// -> Void
127 {
128 print("hello")
129 }
130 // 函数调用
131 sayHello()
132
133 // 有一个参数,无返回值的函数
134 func sayIntValue(a: Int)
135 {
136 print(a)
137 }
138 //调用
139 sayIntValue(100);
140
141 // 有参数有返回值的函数
142 func backSum(a: Int, b: Int) -> Int
143 {
144 return a + b
145 }
146 print(backSum(10, b: 10))
147
148 func backPoint(point1 point1:(Int,Int),point2:(Int,Int)) -> (Int,Int)
149 {
150 return (point2.0 - point1.0,point2.1 - point1.1)
151 }
152 let point1 = (2, 3)
153 let point2 = (10, 10)
154 let point3 = backPoint(point1: point1, point2: point2)
155 print(point3)
156
157 //函数嵌套
158 func sayIntNumber(a: Int)
159 {
160 func sayIntValue()
161 {
162 print(a)
163 }
164 sayIntValue()
165 }
166 sayIntNumber(101)
167
168 // 交互俩个变量的值,必须用inout关键字,传值时,参数前必须加"&"
169 func changeValue(inout a: Int, inout b: Int)
170 {
171 var c = 0
172 c = a
173 a = b
174 b = c
175 }
176 var aValue = 10
177 var bValue = 20
178 changeValue(&aValue, b: &bValue)
179 print(aValue)
180
181
182 // 闭包
183 /*
184 * 闭包书写规范
185 * {(参数名: 参数类型, 参数名: 参数类型) -> 返回值类型 int 实现体
186
187 * }
188 */
189
190 var block: (Int, Int) -> Int = {
191 (a: Int, b: Int) -> Int in
192 return a + b
193 }
194
195 // 闭包调用
196 let block1 = block(10, 10)
197 print(block1)
198
199 //枚举
200 //只有int类型的枚举才有递增
201 enum Direction : Int
202 {
203 case east = 10
204 case south = 20
205 case west
206 case north
207 }
208 let direction1: Direction = Direction.east
209 //rawValue对象对应的值
210 print(direction1.rawValue)
211 //打印对象的哈希值
212 print(direction1.hashValue)
213 print(Direction.south.hashValue)
214
215 enum Student
216 {
217 case description(Int, Double, String)
218 case weight
219 }
220 let 老司机 = Student.description(24, 172, "未知")
221 print(老司机)
222
223 switch 老司机
224 {
225 case Student.description(var age, var height, var sex):
226 print("\(age),\(height),\(sex)")
227 default:
228 print("")
229 }