//可选项1-14
//1.可选项,也叫可选类型,它允许将值设置为nil,在类型名称后面加个问号?来定义一个可选项
var name : String? = "Jack"
name = nil
var age : Int? //默认就是nil
age = 10
age = nil
var array = [1, 15, 40, 29]
func get(_ index: Int) -> Int? {
if index < 0 || index >= array.count {
return nil
}
return array[index]
}
print(get(1))//Optional(15)
print(get(-1))//nil
//2.强制解包,可选项是堆其他类型的一层包装,可以将它理解为一个盒子,如果为nil,那么它是个空盒子,如果不是nil,那么盒子里装的是:被包装类型的数据,如果要从可选项中取出被包装的数据(将盒子里装的东西取出来),需要使用感叹号!进行强制解包
var age0: Int? = 10
var ageInt : Int = age0!
ageInt += 10
//如果对值为nil的可选类型空盒子进行强制解包,将会产生运行时错误
var age1 : Int?
//age1!
//3.判断可选项是否包含值
let number = Int("123")
if number != nil {
print("字符串转换整数成功:\(number!)")//字符串转换整数成功:123
} else {
print("字符串转换整数失败")
}
//4.可选项绑定,用来判断可选项是否包含值,如果包含就自动解包,把值赋值给一个临时的常量let或者变量var,并返回true,否则返回false
if let number0 = Int("123") {
print("字符串转换整数成功:\(number0)")//字符串转换整数成功:123
//number0是强制解包之后的Int值,作用域仅限于这个大括号
} else {
print("字符串转换整数失败")
}
enum Season : Int {
case spring = 1, summer, autumn, winter
}
if let season = Season(rawValue: 6) {
switch season {
case .spring:
print("the season is spring")
default:
print("the season is other")
}
} else {
print("no such season")//
}
//5.等价写法 值绑定和时用,
if let first = Int("4") {
if let second = Int("42") {
if first < second && second < 100 {
print("\(first) < \(second) < 100")
}
}
}
if let first0 = Int("4"),
let second0 = Int("42"),
first0 < second0 && second0 < 100 {
print("\(first0) < \(second0) < 100")
}
//6.while循环中使用可选绑定
//遍历数组,将遇到的整数都加起来,如果遇到负数或非数字停止遍历
var strs = ["10", "20", "abc", "-20", "30"]
var index0 = 0
var sum = 0
while let num = Int(strs[index0]), num > 0 {
sum += num
index0 += 1
}
print(sum)
/*7.空合运算符??
a??b
a是可选项,b是可选项或者不是可选项
b跟a的存储类型必须相同
如果a不为nil,就返回a,如果a为nil,就返回b
如果b不是可选项,返回a时会自动解包
??的运算类型由b决定
*/
let a0: Int? = 1
let b0: Int? = 2
let c0 = a0 ?? b0 //c0是Int?,Optional(1)
let a1: Int? = nil
let b1: Int? = 2
let c1 = a1 ?? b1 //c1是Int?,Optional(2)
let a2: Int? = nil
let b2: Int? = nil
let c2 = a2 ?? b2 //c2是Int?,nil
let a3: Int? = 1
let b3: Int = 2
let c3 = a3 ?? b3 //c3是Int,1
let a4: Int? = nil
let b4: Int = 2
let c4 = a4 ?? b4 //c4是Int,2
let a5: Int? = nil
let b5: Int = 2
//如果不使用??运算符
let c5: Int
if let tmp = a5 {
c5 = tmp
} else {
c5 = b5
}
//8.多个??一起使用
let a6: Int? = 1
let b6: Int? = 2
let c6 = a6 ?? b6 ?? 3 //c6是Int,1
let a7: Int? = nil
let b7: Int? = 2
let c7 = a7 ?? b7 ?? 3 //c7是Int,2
let a8: Int? = nil
let b8: Int? = nil
let c8 = a8 ?? b8 ?? 3 //c8是Int,3
//9.??跟if let 配合使用
let a9: Int? = nil
let b9: Int? = 2
if let c9 = a9 ?? b9 {//类似于if a != nil || b != nil
print(c9)
}
if let c10 = a9, let d10 = b9 {//类似于 if a != nil && b != nil
print(c10)
print(d10)
}
//10. if语句实现登录
func login(_ info: [String : String]) {
let userName: String
if let tmp = info["userName"] {
userName = tmp
} else {
print("请输入用户名")
return
}
let password: String
if let tmp = info["password"] {
password = tmp
} else {
print("请输入密码")
return
}
print("用户名:\(userName)","密码:\(password)","登录ing")
}
login(["userName" : "Jack", "password" : "123456"])
login(["password" : "123456"])
login(["userName" : "Jack"])
/*
11.guard语句 解决上边作用域麻烦的问题
guard 条件 else {
//do something
退出当前作用域
//return、break、continue、throw error
}
1当guard语句的条件为false时,就会执行大括号里面的代码,当guard语句的条件为true时,就会跳过guard语句
2guard语句特别适合用“提前退出”
3当使用guard语句进行可选绑定的时,绑定的变量var、常量let也能在外层作用域中使用
*/
func login0(_ info: [String : String]) {
guard let userName0 = info["userName"] else {
print("请输入用户名")
return
}
guard let password0 = info["password"] else {
print("请输入密码")
return
}
print("用户名:\(userName0)","密码:\(password0)","登录ing")
}
/*
12.隐式解包,在类型后面加个感叹号!定义一个隐式解包可选项。尽量少用!,用?即可安全些,知道就行
在某些情况下,可选项一旦被设定值之后就会一直拥有值,在这种情况下可以去掉检查,也不必每次访问的时候都解包,因为它能确定每次访问的时候都有值。
*/
let num1: Int! = 10
let num2: Int = num1
if num1 != nil {
print(num1 + 6)//16
}
if let num3 = num1 {
print(num3)
}
let num4: Int? = nil
//let num5: Int = num4
//13.字符串插值
//可选项在字符串插值或者直接打印时,编译器会发出警告
var age2: Int? = 10
print("My age is \(age2)") //My age is Optional(10)
//如有警告3中方法解决
print("My age is \(age2!)") // 10
print("My age is \(String(describing: age2))")//Optional(10)
print("My age is \(age2 ?? 0)") //10
//14.多重可选项 容易设置面试题
var num6: Int? = 10
var num7: Int?? = num6
var num8: Int?? = 10
print(num7 == num8) //true
var num9: Int? = nil
var num10: Int?? = num9
var num11: Int?? = nil
print(num10 == num11) //false //易错点
(num10 ?? 1) ?? 2 //2 //易错点 //num9??2 //2
(num11 ?? 1) ?? 2 //1
06-可选项
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 可选项 使用“?”后,值可以为nil 强制解包 确定有值直接使用“!” 可选项绑定 有值就直接解包并赋值给一个变量...
- 疫情过后,快憋坏的你,最想干什么?报复性吃遍全城最牛的馆子?报复性疯狂购物?报复性到处去浪? 其实,大家点赞最多的...