不理解 Objective-C 中的这种语法 [英] Don't understand this syntax in Objective-C

查看:31
本文介绍了不理解 Objective-C 中的这种语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个答案中:https://stackoverflow.com/a/4481896/1486928

有这样一行:

 UITableView *tableView = controller == self.fetchedResultsController ? self.tableView : self.searchDisplayController.searchResultsTableView;

这是我第一次在同一行中看到所有这些符号:/(我是初学者).

It's the first time I see all these symbols in the same line :/ (I'm a beginner).

推荐答案

这是条件运算符.它所做的基本上是根据其条件返回两个值之一:

This is the conditional operator. What it does is basically it returns one of two values based on its condition:

SomeType variable = condition ? valueIfTrue : valueIfFalse;

这可以解释为

SomeType variable;
if (condition) {
    variable = valueIfTrue;
} else {
    variable = valueIfFalse;
}

这里的条件是

controller == self.fetchedResultsController

视图控制器的so等于self.fetchedResultsController,tableView变量将分配给self.tableView,否则将分配给self.searchDisplayController.searchResultsTableView

so of the view controller is equal to self.fetchedResultsController, the tableView variable will be assigned to self.tableView, else it'll be assigned to self.searchDisplayController.searchResultsTableView

这篇关于不理解 Objective-C 中的这种语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆