I love this syntax in Objective-C, where a question mark and colon let you use a backup value:
NSString const name = [self getName] ?: @"backup";
and I want to use the same in Swift, but I get this when I try:
Is there any way to do this in Swift? If not, can I write a custom infix operator to do it?
It's called a null (or nil) coalescing operator, and the Swift syntax is:
let name = getName() ?? "backup";
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