swift optional use details

  • 2020-05-15 02:16:06
  • OfStack

1. Basic usage

Optionality is a special type provided by Swift, which makes it easy to write programs

swift is a strongly typed language, and when we need to use a variable that can be either String or nil, we need to use selectable.


//  Using a special value as "none" can be ambiguous 
//var errorCode = 404
//errorCode = 0
//  Use selectable 
var errorCode:Int? = 404
print( errorCode )
errorCode = nil
print( errorCode )
var color: UIColor? = nil
// Int?  and  Int  not 1 type 
let imInt = 405
errorCode = imInt
//imInt = errorCode
//  Type selectable must be declared explicitly 
//var imOptional = nil
var imOptional: String? = nil

2. Optional unpacking


var errorCode:String? = "404"
print( errorCode )
//  Selectable can not be used directly 
//"The errorCode is" + errorCode
//  Forced to unpack 
"The errorCode is " + errorCode!
//  Forcing unpacking is dangerous 
errorCode = nil
//"The errorCode is" + errorCode!
//  Judgment is not nil
if errorCode != nil{
"The errorCode is " + errorCode!
}
else{
"No error"
}
// if let  Unpack the 
if let unwrappedErrorCode = errorCode{
"The errorCode is " + unwrappedErrorCode
}
else{
"No error"
}
//  You can use the same variable name 
if let errorCode = errorCode{
"The errorCode is " + errorCode
}
else{
"No error"
}
//  use if-let Unpacking multiple variables simultaneously 
var errorMessage:String? = "Not found"
if let errorCode = errorCode{
if let errorMessage = errorMessage{
"The errorCode is " + errorCode + "\nThe errorMessage is " + errorMessage
}
}

3. Multi-layer unpacking (** swift 3.0 changed)


// Multilayer unpacking ( swift 2.0 Use) 
if let errorCode = errorCode , errorMessage = errorMessage{
"The errorCode is " + errorCode + "\nThe errorMessage is " + errorMessage
}
// The above multi-layer unpacking method is in swift3.0 Should be written as 
if let errorCode = errorCode , let errorMessage = errorMessage{
"The errorCode is " + errorCode + "\nThe errorMessage is " + errorMessage
}
// where ( swift 2.0 Use) 
if let errorCode = errorCode , errorMessage = errorMessage where errorCode == "404"{
print("Page not found")
}
// in swift3.0 Should be written as  
// where
if let errorCode = errorCode , let errorMessage = errorMessage , errorCode == "404"{
print("Page not found")
}

4. Use of Optional chaining

But selection & # 63; This is called Optional, chaining

This approach is an attempt to unpack and keep the code simple


var errorMessage: String? = "Not Found"
if let errorMessage = errorMessage{
errorMessage.uppercased()
}
// use  Optional chaining
errorMessage?.uppercased()
var uppercaseErrorMessage = errorMessage?.uppercased()
//uppercaseErrorMessage As a selection 
if let errorMessage = errorMessage?.uppercased(){
errorMessage
}
// Optional chaining  You can also use !
let uppercaseErrorMessage2 = errorMessage!.uppercased()
// At this time  uppercaseErrorMessage2  for String Type. At this point for forced unpacking, prone to errors 

5. Use of Nil-Coalescing


// Nil-Coalescing
var errorMessage: String? = nil
let message: String
if let errorMessage = errorMessage{
message = errorMessage
}
else{
message = "No error"
}
//  use 3 Unary operator 
let message2 = errorMessage == nil ? "No error" : errorMessage!
//  Pay attention to :  At this time to use errorMessage You need to force unpacking . In order to make sure message2 is 1 a String Rather than String?
// 3 Unary operator ?: The essence is 1 a if else
//  use  ??
let message3 = errorMessage ?? "No error"
//  Pay attention to :  At this time to use errorMessage There is no need to force unpacking . 
// ?? The symbol already guarantees access errorMessage when , errorMessage not nil
// Swift It will be unpacked automatically for us ,  ensure message2 Is always 1 a String,  Rather than String?
// This is called  Nil-Coalescing

Related articles: