John Fowler
John Fowler

Reputation: 3281

Is there an Objective-C equivalent for JavaScript's ||

In JavaScript, the || operator will return the first non-false-evaluating operand. For example:

var x = null;
var y = "yup";
var z = x || y; // z is now "yup"

Is there an Objective-C equivalent for this?

For example:

NSString *x = nil;
NSString *y = @"yup";
NSString *z = x ... y; // z should now be @"yup", if "..." were an operator

Please Note: The '||' does short-circuit in JavaScript

Upvotes: 1

Views: 118

Answers (1)

Catfish_Man
Catfish_Man

Reputation: 41821

There's a compiler extension to C (and therefore ObjC) that does what you want:

z = x ?: y;

(for a standard C/ObjC version: z = x ? x : y)

Upvotes: 3

Related Questions