简体   繁体   中英

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?

That is a so-called ternary operator

Ternary operators in C have the following pattern condition ? true-expression : false-expression 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" .

您还可以使用它来打印BOOL的值,例如:

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

那是三元运算符

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM