Swift has the nil-coalescing operator a ?? b
which is shorthand for a != nil ? a : b
. Does Swift have the opposite operator, a shorthand for a == nil ? a : b
or in other words, a == nil ? nil : b
?
I would use it to map an optional value to something else, like so:
let x = dict["key"] != nil ? mapValue(dict["key"]) : nil
// ideally: let x = dict["key"] ¿¿ mapValue(dict["key"])
Swift's nil coalescing operator helps you solve this problem by either unwrapping an optional if it has a value, or providing a default if the optional is empty. Because name is an optional string, we need to unwrap it safely to ensure it has a meaningful value.
The nil-coalescing and ternary conditional operators are what's known as syntatic sugar. They sugar-coat verbose code in more concise code, and make your Swift code more readable.
Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil . If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil , the property, method, or subscript call returns nil .
The nil-coalescing operator provides a more elegant way to encapsulate this conditional checking and unwrapping in a concise and readable form. If the value of a is non- nil, the value of b isn’t evaluated. This is known as short-circuit evaluation.
You can opt in to value overflow behavior by using Swift’s overflow operators, as described in Overflow Operators. Swift also provides range operators that aren’t found in C, such as a..<b and a...b, as a shortcut for expressing a range of values. This chapter describes the common operators in Swift.
The remainder operator ( %) is also known as a modulo operator in other languages. However, its behavior in Swift for negative numbers means that, strictly speaking, it’s a remainder rather than a modulo operation. Here’s how the remainder operator works. To calculate 9 % 4, you first work out how many 4 s will fit inside 9:
The code above uses the ternary conditional operator and forced unwrapping ( a!) to access the value wrapped inside a when a isn’t nil, and to return b otherwise. The nil-coalescing operator provides a more elegant way to encapsulate this conditional checking and unwrapping in a concise and readable form.
dict["key"]
returns an optional, and you want to map that value to another value if it is present, or get nil
otherwise. That is exactly what Optional.map()
is for:
Evaluates the given closure when this
Optional
instance is notnil
, passing the unwrapped value as a parameter.
In your case:
let x = dict["key"].map { mapValue($0) }
or simply
let x = dict["key"].map(mapValue)
This also has the advantage over
let x = dict["key"] != nil ? mapValue(dict["key"]) : nil
// or the hypothetical
let x = dict["key"] ¿¿ mapValue(dict["key"])
that dict["key"]
is evaluated only once, not twice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With