<分区>
标签 objective-c ios
<分区>
Possible Duplicate:
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 中? 谢谢!!
最佳答案
这是一个三元运算符。
例子:
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)
关于objective-c - 谁能帮我理解这个? ------ ( )? []:[ ] in objectiveC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12605834/