objective-c - 谁能帮我理解这个? ------ ( )? []:[ ] in objectiveC

标签 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/

上一篇:ios - 你如何在没有 iPhone 的情况下调试 iPhone 应用程序?

下一篇:ios - 使用检索到的核心数据

相关文章:

objective-c - Instruments 中的内存分配过多是否不好?

ios - 如果将 UIButton 添加为 UIImageView 的 subview ,则单击它不起作用

iphone - 滑动以从 tableView 中删除记录

ios - 当在 DetailViewController 中显示时,TableView 的自定义 UITableViewCell 消失

ios - 如何以模态方式呈现透明的 UINavigationController + root?

ios - UIButton 子类中的 imageview.transform 奇怪行为

ios - CATransform3D 打破图层顺序?

ios - 在 CoreData 中创建实例

ios - 如何调整导航栏中按钮的垂直位置?

ios - 从应用商店更新后应用崩溃。问题可能与核心数据有关?