Clicky

iOS Dev Nugget 61 Null Coalescing Operator

.

Need to run a code review on your codebase? Hire me

The what?

We sometimes write code like:

NSString* someValue;
//...
NSString* val;
if (someValue) {
    val = someValue;
} else {
    val = @"some default";
}

or similarly, using the tenary operator:

NSString* someValue;
//...
NSString* val = someValue? someValue: @"some default";

You can leave out the middle operand of the tenary operator like this and it'll work as before:

NSString* someValue;
//...
NSString* val = someValue?: @"some default";

This special form of the tenary operator is known as the null coalescing operator in some languages.


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues