Optional
- Optional, also generally called optional type, allows the value to be set to nil
- Add a question mark ? after the type name to define an option
var name: String? = "CSDN"
name = nil
var age: Int? //默认就是nil
age = 30
age = nil
var array = [2, 4, 6, 8]
func getNumber(_ index: Int) -> Int? {
if index < 0 || index >= array.count {
return nil
}
return array[index]
}
print(getNumber(1)) //Optional(4)
print(getNumber(-1)) //nil
print(getNumber(4)) //nil
Forced Unwrapping
- Optional options are a layer of packaging for other types, which can be understood as a box
- If nil , then it is an empty box
- If it is not nil , then the box contains: data of the packed type
var age: Int? //默认就是nil
age = 10
age = nil
- If you want to take out the packed data from the optional items (take out the contents of the box), you need to use the exclamation point ! to force unpacking
var age: Int? = 20
let ageInt: Int = age!
print(ageInt) //20
- If you force unpack a nil optional (empty box), a runtime error will occur.
var age: Int?
age!
Fatal error: Unexpectedly found nil while unwrapping an Optional value
Determine whether an optional item contains a value
let number = Int("123")
if number != nil {
print("字符串转成数字成功为:\(number!)")
}else{
print("字符串转成数字失败")
}
// 字符串转成数字成功为:123
Optional Binding
- You can use optional binding to determine whether an optional item contains a value
- If included, automatically unpack, assign the value to a temporary constant ( let ) or variable ( var ), and return true , otherwise return false
if let number = Int("123") {
print("字符串转成数字成功为:\(number)")
// number 是强制解包之后的Int值
// number 作用域仅限于这个大括号
}else{
print("字符串转成数字失败")
}
// 字符串转成数字成功为:123
enum Season : Int {
case spring = 1, summer, autumn, winter
}
if let season = Season(rawValue: 8){
switch season {
case .spring:
print("the season is spring")
default:
print("the season is other")
}
} else {
print("no such season")
}
// no such season
Equivalent writing
if let first = Int("5") {
if let second = Int("45") {
if first < second && second < 50 {
print("\(first) < \(second) < 50")
}
}
}
// 5 < 45 < 50
if let first = Int("5"),
let second = Int("45"),
first < second && second < 50 {
print("\(first) < \(second) < 50")
}
// 5 < 45 < 50
Using optional bindings in while loops
//遍历数组,将遇到 的正整数都加起来,如果遇到负数或者非数字,则停止遍历
var array = ["10", "20", "30", "ab", "-20", "40"]
var index = 0
var sum = 0
while let num = Int(array[index]), num > 0 {
sum += num
index += 1
}
print(sum)
Nil - Coalescing Operator??
public func ?? <T>(optional: T?, defaultValue: @autoclosure () throws -> T?) rethrows -> T?
public func ?? <T>(optional: T?, defaultValue: @autoclosure () throws -> T) rethrows -> T
- a ?? b
- a is optional
- b is optional or not optional
- The storage types of b and a must be the same
- If a is not nil , return a
- If a is nil , return b
- If b is not optional, it will be automatically unpacked when returning a
let a: Int? = 1
let b: Int? = 2
let c = a ?? b // c是Int? , Optional(1)
let a: Int? = nil
let b: Int? = 2
let c = a ?? b // c是Int? , Optional(2)
let a: Int? = nil
let b: Int? = nil
let c = a ?? b // c是Int? , nil
let a: Int? = 1
let b: Int = 2
let c = a ?? b // c是Int , 1
let a: Int? = nil
let b: Int = 2
let c = a ?? b // c是Int , 2
let a: Int? = nil
let b: Int = 2
//如果不使用 ?? 运算符
let c: Int
if let tmp = a {
c = tmp
} else {
c = b
}
Use multiple?? together
let a: Int? = 1
let b: Int? = 2
let c = a ?? b ?? 3 // c是Int , 1
let a: Int? = nil
let b: Int? = 2
let c = a ?? b ?? 3 // c是Int , 2
let a: Int? = nil
let b: Int? = nil
let c = a ?? b ?? 3 // c是Int , 3
?? Used in conjunction with if let
let a: Int? = nil
let b: Int? = 2
if let c = a ?? b {
print(c)
}
//类似于 if a != nil || b != nil
let a: Int? = nil
let b: Int? = 2
if let c = a , let d = b {
print(c)
print(d)
}
// 类似于 if a != nil && b != nil
if statement implements login
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
}
// if username ....
// if password ....
print("用户名:\(username)", "密码:\(password)", "登陆ing")
}
login(["username" : "jack", "password" : "123456"]) // 用户名:jack 密码:123456 登陆ing
login(["password" : "123456"]) // 请输入密码
login(["username" : "jack"]) // 请输入用户名
guard statement
guard 条件 else {
// do something....
退出当前作用域
//return、break、continue、throw error
}
- When the condition of the guard statement is false , the code inside the curly brackets will be executed.
- When the condition of the guard statement is true, the guard statement will be skipped
- The guard statement is particularly suitable for "early exit"
- When using the guard statement for optional binding, the bound constants ( let ) and variables ( var ) can also be used in the outer scope.
func login(_ info: [String : String]){
guard let username = info["username"] else {
print("请输入用户名")
return
}
guard let password = info["password"] else {
print("请输入密码")
return
}
// if username ...
// if password ...
print("用户名:\(username)","密码:\(password)","登录ing")
}
login(["username" : "jack", "password" : "123456"]) // 用户名:jack 密码:123456 登陆ing
login(["password" : "123456"]) // 请输入密码
login(["username" : "jack"]) // 请输入用户名
Implicitly Unwrapped Optional
- In some cases, once an optional is set, it retains its value.
- In this case, you can remove the check and unpack it every time it is accessed, because it will ensure that the value is available every time it is accessed.
- You can add an exclamation mark after the type! Define an implicit unpacking option
let num1: Int! = 10
let num2: Int = num1
if num1 != nil {
print(num1 + 6) // 16
}
if let num3 = num1 {
print(num3) // 10
}
let num1: Int! = nil
//Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
let num2: Int = num1
String interpolation
- The compiler will issue a warning when optionally interpolating strings or printing directly.
var age: Int? = 10
print("My age is \(age)")
- There are at least 3 ways to eliminate the warning
print("My age is \(age!)")
//My age is 10
print("My age is \(String(describing: age))")
//My age is Optional(10)
print("My age is \(age ?? 0)")
//My age is 10
Multiple options
var num1: Int? = 10
var num2: Int?? = num1
var num3: Int?? = 10
print(num2 == num3) // true
- You can use the lldb command frame variable –R or fr v –R to see the difference.
var num1: Int? = nil
var num2: Int?? = num1
var num3: Int?? = nil
print(num2 == num3) // false
(num2 ?? 1) ?? 2 // 2
(num3 ?? 1) ?? 2 // 1