ios swift in? ? Empty combined operator (Nil Coalescing Operator)

Empty combined operator (a ?? b) will be empty alternative type of a determination, if a is not nil is returned, and otherwise returns to a default value b. Optional expression must be a type. The default value of the type b and type must be stored in a consistent value.

Empty combined operator is actually a ?? b a brief presentation of the following three methods of operation mesh

a != nil  ? a!:b

It is equivalent between the two

E.g:

let defaultColorName = "red"

var userDefinedColorName: String? // The default value is nil

var colorNameToUser =userDefinedColorName ?? defaultColorName

// userDefinedColorName is empty so colorNameToUser value "red"

If you assign a non-null value (non-nil) to userDefinedColorName, bonding operation is performed again empty, the operation results in the packet in userDefaultColorName value, rather than the default value.

userDefinedColorName = "green"

colorNameToUser =userDefinedColorName ?? defaultColorName

// userDefinedColorName non-empty, so colorNameToUser value of "green"

Guess you like

Origin www.cnblogs.com/miaolegewangde/p/11590356.html