可能重复:
What does the question mark and the colon (?: ternary operator) mean in objective-c?
NSString *requestString = (self.isFirstTimeDownload) ? [NSString stringWithFormat:[self.commonModel.apiURLs objectForKey:@"updateNewsVerPOST"],@""] : [NSString stringWithFormat:[self.commonModel.apiURLs objectForKey:@"updateNewsVerPOST"], [[NSUserDefaults standardUserDefaults] objectForKey:@"localnewsupdate"]];
任何人都可以帮我理解这是什么()?和:在Objective-c? 谢谢!!
答案 0 :(得分:4)
那是三元运营商。
示例:
bool foo(int i)
{
if ( i > 5 )
return true;
else
return false;
}
相当于
bool foo(int i)
{
return ( i > 5 ) ? true : false;
}
您可以省略第一个操作数:x ? : b
在这种情况下,当x为非零时,表达式的值为x,否则为b。例如:
int i = 1;
i = 2 ? : 3; // equivalent to i = 2; (because 2 is non zero)
i = YES ? : 3; // equivalent to i = 1; (because YES is 0x01, which is not zero)
答案 1 :(得分:0)
NSString *requestString = ( boolean condition ) ? @"valueIfTrue" : @"valueIfFalse";