可能重复:
What does the question mark and the colon (?: ternary operator) mean in objective-c?
我知道我们将oldRow
设置为等于某个索引路径。我从未见过这种语法,也无法在我正在使用的书中找到解释。以下代码中?
的目的是什么?这段代码到底是做什么的?
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
答案 0 :(得分:7)
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
相当于:
int oldrow = 0;
if (lastIndexPath != nil)
oldRow = [lastIndexPath row];
else
oldRow = -1;
该语法称为三元运算符,遵循以下语法:
condition ? trueValue : falseValue;
i.e oldRow = (if lastIndexPath is not nil ? do this : if it isn't do this);
答案 1 :(得分:2)
这是if语句的简写。基本上它与:
相同int oldRow;
if(lastIndexPath != nil)
{
oldRow = [lastIndexPath row];
}
else
{
oldRow = -1;
}
条件分配非常方便
答案 2 :(得分:1)
此代码等于此代码
int oldRow;
if (lastIndexPath != nil)
oldRow = [lastIndexPaht row];
else
oldRow = -1;