ma11hew28 ma11hew28 - 3 years ago 103
Swift Question

Does Swift have an “or equals” function like ||= in Ruby?

In Swift, how do I set an optional

but only if it's

I know about Swift's nil coalescing operator:

foo = foo ?? "Hello"
is an optional

But, is there a better way, like in Ruby?

foo ||= "Hello"

Answer Source

Such an operator was considered and rejected:

The rational behind the rejection is supposed to be in the swift-evolution email list, but the link attached to the above points to SE-0025 instead of 24.

Here is the link to the rational (Edited because of Kenneth Bruno's comment blow):

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download