Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift reverse nil-coalescenting operator?

Tags:

swift

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"])
like image 575
Markus Meskanen Avatar asked May 26 '20 10:05

Markus Meskanen


People also ask

What is nil coalescing operator Swift?

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.

What is nil coalescing & ternary operator?

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.

What is optional chaining nil coalescing operator?

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 .

What is nil coalescing operator in JavaScript?

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.

How do you overflow a value in Swift?

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.

What is the remainder operator in Swift for negative numbers?

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:

What is the use of Nil-coalescing operator?

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.


1 Answers

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 not nil, 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.

like image 153
Martin R Avatar answered Oct 16 '22 19:10

Martin R