swift5.0 基础(二)
隐式转换
只有oc有 swift并不支持
swift 类型推导 但不支持隐式转换 只能同类型运算
错误示范:
1
2
3
4
5
6
7
8
9
10let a = 10 let b = 4.5 let result = a + b 将a转换浮点型 let a = 10 let b = 4.5 let tempA = Double(a) let result = tempA + b
三目运算符
和oc 是一样的
let a = 100
let b = 101
let result = a > b ? a : b
swift 特有2.0以后才有 guard 和if相似 提高可读性
guard 条件表达式 else {
//条件语句
break
}
语句组
当条件表达式为true时跳过else,执行语句组
当条件表达式为false时执行else,跳转语句一般为return、break、continue、throw
当多种条件嵌套判断时 guard可读性相对高
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18例如:上火车的条件 if 有票 { if 有身份证 { 上车 } else { 拿身份证 } } else { 买票 } guard 有票 else { 买票 } guard 有身份证 else { 拿身份证 }
switch
1
2
3
4
5
6
7
8
9let sex = 1 switch sex { case 1: print("男") default: print("女") }
补充:
1.case穿透 fallthrough
2.case后面可以判断多个条件以“,”分割
1
2
3
4
5
6
7switch sex { case 0, 1: print("正常") default: print("其他 ") }
3.可以判断字符串 oc不支持
1
2
3
4
5
6
7
8
9
10
11
12
13let a = 10 let b = 20 let opration = "+" switch opration { case "+" result = a + b case "-" result = a - b default print("非法操作") }
4.可以判断区间 oc不支持
闭区间 0…10 开区间0…<10
ps:标识符不需要使用,可以使用_来代替
while后面的()可以省略
while后面的判断没有非0即真
字符串
oc中NSString在Swift是String
oc中继承NSObject
Swift String是一个结构体,性能更高,支持直接遍历 String和NSString之间无缝转换
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定义字符串 let str = "hello" 遍历 for c in str.chatacters { print(c) } 字符串拼接 let str1 = "abc" let str2 = "def" let str3 = str1 + str2 字符串和其他标识符之间拼接 let name = "tom" let age = 12 let height = 1.78 let info = "name is (name) age is (age) height is (height)" 拼接字符串 格式化 let min = 2 let sec = 12 String(format:"%02d:%02d", arguments: [min,sec]) //02:12 字符串截取 let url = "www.bai.com" //将string转换NSString (string as nsstring) let header = (url as NSString).substring(to: 3) let middele = (url as NSString).substring(with: NSMakeRange(4, 3)) let end = (url as NSString).substring(from: 7)
数组
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不可变数组 let array = ["a","b","c"] 可变数组 var arryaM = Array<String>() //少 var arrayM = [String]() //等同上面 //增 arrayM.append("d") arrayM.append("e") arrayM.append("l") //删 arrayM.remove(at: 0) //改 arrayM[0] = "f" //取 arrayM[0] //遍历 for i in 0..<arrayM.count { print(arrayM[i]) } for name in arrayM { print(name) } //遍历前两位 for i in 0..<2 { print(arrayM[i]) } for namw in arrayM[0..<2] { print(namw) }
数组的合并
let resultArray = arrayA + arryB
相通类型的数组才可以合并,不同类型不能相加合并
字典
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
29Dictionary 不可变 let dictory = ["name":"lili", "age":20, "height":1.88] as [String : Any] 可变 var dicM = [String : AnyObject]() //AnyObject指定类型 NSObject 一般创建对象 /可变字典基本操作 //添加元素 dicM["name"] = "A" as AnyObject dicM["age"] = 19 as AnyObject //删除元素 dicM.removeValue(forKey: "age") //修改元素 dicM["name"] = "B" as AnyObject //如果有这个key 直接修改value 没有添加 //获取某一个元素 dicM["name"] //遍历字典 for key in dicM.keys { print(key) } for (key,value) in dicM { print("key is (key) value is (value)") } //合并字典 let dic1 = ["name":"YY", "age":"19"] as [String : Any] var dic2 = ["height":"12"] for (key,value) in dic1 { dic2[key] = (value as! String) }
元组
swift持有 oc并没有相关类型
元组是一种数据结构 类似于数组或字典
可用于定义一组数据
组成元组类型的数据称为”元素“
最后
以上就是爱撒娇灯泡最近收集整理的关于swift5.0 基础(一)数组 字典 字符串等的全部内容,更多相关swift5.0内容请搜索靠谱客的其他文章。
发表评论 取消回复