缩短问号冒号?:Objective-C语法?

时间:2021-09-14 19:12:58

Xcode does not give an error of my (thought-to-be) typo:

Xcode没有给出我(想要)打错的错误:

 NSString *theme = [[NSUserDefaults standardUserDefaults] objectForKey:@"theme"];
 NSLog(@"Theme: %@", theme ?: @"Default");

It turns out:

事实证明:

 NSLog(@"Theme: %@", theme ?: @"Default");

works same as:

是一样的:

 NSLog(@"Theme: %@", theme ? theme : @"Default");

Is the above shorten syntax good for gcc only? Or it is part of Objective-C?

上述缩短语法仅适用于gcc吗?或者它是Objective-C的一部分?

1 个解决方案

#1


17  

It's a GNU extension to the conditional expression in C:

它是对C中的条件表达式的GNU扩展:

From here:

从这里开始:

A GNU extension to C allows omitting the second operand, and using implicitly the first operand as the second also:

GNU扩展到C允许省略第二个操作数,并且隐式地使用第一个操作数作为第二个操作数:

a = x ? : y;

#1


17  

It's a GNU extension to the conditional expression in C:

它是对C中的条件表达式的GNU扩展:

From here:

从这里开始:

A GNU extension to C allows omitting the second operand, and using implicitly the first operand as the second also:

GNU扩展到C允许省略第二个操作数,并且隐式地使用第一个操作数作为第二个操作数:

a = x ? : y;