chicobermuda chicobermuda - 2 months ago 12
iOS Question

Ternary Conditional Operator for nil/not nil

I can use the ternary conditional operator for an

if {} else {}
statement like this:
a ? x : y
, or
question ? answer1 : answer2
.

Is it possible to use this format to check if, instead of whether
a
is
true
or
false
,
a == nil
or
a != nil
?




UPDATE: This was arguably the biggest brain fart of my career.

Answer

You can do this:

(a == nil) ? x : y

(Parentheses are not required but may make the code clearer.)

You can do this if you want something more confusing:

a.map { _ in x } ?? y