This article focuses on the basic features and advantages of swift code compared to other languages

optional

The ## definition is also called an optional type, and it allows you to set the value to nil by saying? To define the optionsCopy the code
    var a:Int?              var b:Int              
    var a:Int? = nil        var b:Int = 0
Copy the code

The one on the left is equivalent, the one on the right is different. Take 🌰 :

‘nil’ is incompatible with return type ‘Int’

Forced to unpack

The alternative is a layer of wrapping for other types, you can think of it as a box, if it's nil, it's an empty box, if it's not nil, it's got wrapped data in itCopy the code
    var age:Int?                            
    age = 10
    age = nil
Copy the code

The above code can be interpreted as the following figure:

To extract data from an optional is called unpacking and requires an exclamation point! To force unpackCopy the code
    var age:Int? = 10                            
    var num = age + 11
Copy the code
Let's see, what's wrong with the code? Value of optional type 'Int? 'Must be unwrapped to a value of type 'Int' you assign 11 to num by age, but num doesn't know if you have a value in it. Optional options may be nil, so force unwrapped!Copy the code
    var age:Int? = 10                            
    var num = age! + 11
Copy the code
Age: Found optional(10), so forcing unpacking is just taking the value out of the box and not changing itCopy the code

A runtime error occurs if the value is nil, that is, the empty box is forced to unpack

Determines whether the optional option contains a value

    var num = Int("xixi123")
Copy the code

Int forced unpack = 0; Of course not! Because you could have an Int and it could be 0, minus 1.

Optional binding

Optional binding can be used to determine if the optional contains a value and if so it is automatically unpacked and assigned to a temporary constant let or variable var, returning true/falseCopy the code
    if let num = Int("123") {
    print("String conversion to integer succeeded\(num)")}else{
    print("String to integer failed!")}Copy the code
Num = 123 (optional)Copy the code

Equivalent writing

    if let number = Int("4") {
        if let count = Int("32") {if number < count && count < 100{
                print("\(number) <\ [count)"100")}}}/ / 4 < 32 < 100
Copy the code

Is equivalent to:

    if  let number = Int("4"),
        let count = Int("32"),
        number < count && count < 100{
           print("\(number) <\ [count)"100")}/ / 4 < 32 < 100
Copy the code

Null merge operator??

a?? bCopy the code

Note:

If a is not nil, return A; if a is nil, return B 4. If b is not optional, return a will be unpacked automaticallyCopy the code

Built-in functions take a look:

If B is optional, it’s optional. If B is not optional, it’s not optional

An 🌰 :

?? Use with if let

    let a:Int? = nil
    let b:Int? = 12
    if let c = a ?? b{
        print(c)}// Similar to if a! =nil || b! =nil
    if let c = a, d = b{
        print(c)
        print(d)
    }
    // Similar to if a! =nil && b! =nil
Copy the code

Above code, first look at a?? B,b is optional, so the result is optional, so you can use optional binding

You can use this one-two punch later if there might be nil in the variable

Guard statement

guardconditionselse {
    // do something...Exits the current scope//return,break,continue,throw error
}
Copy the code

When the guard condition is false, the statements inside the braces are executed. When the GUARD condition is true, the guard statement is jumped. The Guard statement is particularly good for exiting early. The var variable can also be used in outer scopes

Func login (_ info:[String:String]){guard let username = info["username"] else{print(" please enter username") return} guard let Password = info["password"] else{print(" Please enter password") return}}Copy the code

Implicit solution package

let num11 : Int? = 10
let num22 : Int = num11
Copy the code

In some cases, once an option is set to a value, it is always owned. It does not need to be unpacked every time it is accessed, because it has a value every time it is accessed

let num11 : Int! = 10
let num22 : Int = num11
Copy the code

? To change! To forcibly unpack

Multiple options