2012-09-26 65 views
0

可能重复:
What does the question mark and the colon (?: ternary operator) mean in objective-c?任何人都可以帮助我理解这一点吗? ------()? []:[]中的ObjectiveC

NSString *requestString = (self.isFirstTimeDownload) ? [NSString stringWithFormat:[self.commonModel.apiURLs objectForKey:@"updateNewsVerPOST"],@""] : [NSString stringWithFormat:[self.commonModel.apiURLs objectForKey:@"updateNewsVerPOST"], [[NSUserDefaults standardUserDefaults] objectForKey:@"localnewsupdate"]]; 

谁能帮我明白这是()?和:在Objective-c中? 谢谢!

+3

的可能重复[什么问号和冒号(?:三元运算符)的意思是在Objective-C?(HTTP://计算器.com/q/2595392 /),[这是什么意思:NSString * string = NO? @“aaa”:@“bbb”;](http://stackoverflow.com/q/8290073/),[不明白此语法](http://stackoverflow.com/q/12132665/),[ Objective-C运算符(?)和(:)](http://stackoverflow.com/q/11705848/),[以下语句中的“?”是什么意思?](http://stackoverflow.com/ q/5832134 /),[无效? @“inactive”:@“active”语法?](http://stackoverflow.com/q/10239632/), –

回答

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) 
+0

@Jano感谢您提供额外的信息。 – Mahesh

相关问题