Swift从入门到精通第四篇 - 可选项 解包 guard
可选项 解包 guard(学习笔记)
环境Xcode 11.0 beta4 swift 5.1
-
可选项的基本用法
- 可选项,一般也叫可选类型,值可以设置为
nil - 定义一个可选项是在类型后面加个
?var name: String? = "Me" name = nil var age: Int? // 默认是nil age = 99 age = nil - Swift的nil不等于Objective-C中的nil。在Objective-C中,nil是指向不存在对象的指针。在Swift中,nil不是指针,它是某种类型缺少具体的值。任何可选类型都可以设置为nil,而不仅仅是对象类型。
- 可选项,一般也叫可选类型,值可以设置为
-
强制解包(Forced Unwrapping)
- 可以配合
if语句使用,可以通过==或者!=,如果一个可选项有值,将被认为!= nillet possibleNumber = "123" let convertedNumber = Int(possibleNumber) // convertedNumber 被编译器推导为类型 "Int?" 或者 "optional Int" if convertedNumber != nil { print("convertedNumber contains some integer value.") } // Prints "convertedNumber contains some integer value." - 可选项强制解包还可以使用
!,如果此时可选项值为nil,将会产生运行时错误var age: Int? = 10 let age2: Int = age!
- 可以配合
-
可选项绑定 (Optional Binding)
- 可以用来判断可选项是否包含值,如果有值,赋值给一个临时的变量(可以用
letorvar来修饰)并返回true,否则返回falseif let actualNumber = Int(possibleNumber) { print("The string \"\(possibleNumber)\" has an integer value of \(actualNumber)") } else { print("The string \"\(possibleNumber)\" could not be converted to an integer") } // "123"可以转换成Int类型数值123 // Prints "The string "123" has an integer value of 123" if语句中多个可选项是否有值的判断,多个条件可以用,分割,这种情况等价多个条件使用&&if let firstNumber = Int("4"), let secondNumber = Int("42"), firstNumber < secondNumber && secondNumber < 100 { print("\(firstNumber) < \(secondNumber) < 100") } // Prints "4 < 42 < 100" if let firstNumber = Int("4") { if let secondNumber = Int("42") { if firstNumber < secondNumber && secondNumber < 100 { print("\(firstNumber) < \(secondNumber) < 100") } } } // Prints "4 < 42 < 100" // 以上两种写法是等价的- 在if语句中使用可选绑定创建的常量和变量只能在if语句的主体中使用。相反,使用guard语句创建的常量和变量可以在guard语句后面的代码行中使用
- 可以用来判断可选项是否包含值,如果有值,赋值给一个临时的变量(可以用
-
隐式解包(Implicitly Unwrapped )
- 在声明可选项时在其类型后面加上
!,这样每次使用时不需要在变量后面加上!,你可以认为是可选项在使用时自动解包 - 如果隐式可选项的值为
nil,此时如果使用一样会触发一个运行时的错误,与强制解包一个nil值是一样的 - 你也可以把隐式解包当做一个普通的可选项,用
if高速公路来判断 - 隐式解包的可选项也可以用像普通可选项绑定一样绑定
// 1 let possibleString: String? = "An optional string." let forcedString: String = possibleString! // 强制解包 let assumedString: String! = "An implicitly unwrapped optional string." let implicitString: String = assumedString // 不需强制解包 // 2 let num1: Int! = nil let num2: Int = num1 // Fatal error // 3 if assumedString != nil { print(assumedString!) } // Prints "An implicitly unwrapped optional string." // 4 if let definiteString = assumedString { print(definiteString) } // Prints "An implicitly unwrapped optional string."
- 在声明可选项时在其类型后面加上
-
多重可选项
- 下图中 num1 是Int? num2和num3都是Int??类型,因为它们都有值,所以结果都为true
![多重可选项01 多重可选项]()
- 下图中 num1 是Int?,但值为nil, num2可以是在nil层包装一层,是some有值,但解包之后也是nil,num3虽然是??,但就是一个nil且与num1、num2类型都不相同
![多重可选项02 多重可选项]()
- 下图中 num1 是Int? num2和num3都是Int??类型,因为它们都有值,所以结果都为true
-
guard关键字guard基本格式guard 条件 else { // do something // 退出当前作用域 // return, break, continue, or throw, fatalError(_:file:line:) }guard语句条件为false时,执行else大括号里的语句,条件为true时,执行大括号后面的语句guard条件进行可选项绑定时,绑定的变量可以用let、var来修饰,也能在外层作用域中使用func greet(person: [String: String]) { guard let name = person["name"] else { return } print("Hello \(name)!") guard let location = person["location"] else { print("I hope the weather is nice near you.") return } print("I hope the weather is nice in \(location).") } greet(person: ["name": "John"]) // Prints "Hello John!" // Prints "I hope the weather is nice near you." greet(person: ["name": "Jane", "location": "Cupertino"]) // Prints "Hello Jane!" // Prints "I hope the weather is nice in Cupertino."



浙公网安备 33010602011771号