Josh Sherick
Josh Sherick

Reputation: 2161

What does this mean: NSString *string = NO ? @"aaa" : @"bbb";

I was reading over the Dropbox API and I found this line:

NSString* title = [[DBSession sharedSession] isLinked] ? @"Unlink Dropbox" : @"Link Dropbox";

I've never seen that syntax before? What is it called and what does it mean? I can tell what it does just from looking at it but could someone tell me about it?

Upvotes: 0

Views: 336

Answers (3)

Jan Dragsbaek
Jan Dragsbaek

Reputation: 8101

That is the Ternary Operator.

Upvotes: 0

Mat
Mat

Reputation: 7633

You can also use this to print BOOLs values like:

NSLog(@"%@", boolVal ? @"YES" : @"NO");

Upvotes: 2

Regexident
Regexident

Reputation: 29552

That is a so-called ternary operator

Ternary operators in C have the following pattern condition ? true-expression : false-expression.

If condition evaluates to YES, then true-expression gets evaluated, otherwise false-expression.

In your particular case title would get assigned to @"Unlink Dropbox" if [[DBSession sharedSession] isLinked] returns YES, otherwise @"Link Dropbox".

Upvotes: 12

Related Questions