Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS中的复杂代码_Ios_Objective C - Fatal编程技术网

iOS中的复杂代码

iOS中的复杂代码,ios,objective-c,Ios,Objective C,在这里,我被以下代码块卡住了 self.isFiltered?[self.filteredCategories count]:[self.categories count] 这里,isfilteredcategories是一个bool,filteredCategories是一个可变数组,categories是一个数组。此行返回一个整数。我不明白这条线是怎么运作的。谢谢同样感谢: int someVariable = 0; if(self.isFiltered) { someVariable

在这里,我被以下代码块卡住了

self.isFiltered?[self.filteredCategories count]:[self.categories count]
这里,
isfilteredcategories
是一个bool,
filteredCategories
是一个可变数组,categories是一个数组。此行返回一个整数。我不明白这条线是怎么运作的。谢谢

同样感谢:

int someVariable = 0;
if(self.isFiltered) {
  someVariable = [self.filteredCategories count];
} else {
  someVariable = [self.categories count];
}

在您的代码中,它只是另一种形式。

您可以使用普通if-else来简化事情。如果有疑问,请使用标准if-else语法

if(self.isFiltered){
    [self.filteredCategories count]
}
else{
    [self.categories count]
}
这是一个好主意。假设你有一个
,如果是这样的话:

if (condition)
    var = one thing
else
    var = other thing
作为三元数,这将是

var = condition ? one thing : other thing

因此,在您的情况下,如果过滤,它会将您的变量设置为过滤器计数,如果未过滤,它会将变量设置为完整类别计数。

这并不难,它是if/else的“高级”版本。它表示如果变量
isfilteredcategories
设置为YES,它将返回
filteredCategories
数组中的元素数,但如果设置为NO,它将返回
类别中的元素数

我在表视图委托方法(-(NSInteger)tableView:(UITableView*)中有这一行tableView numberOfRowsInSection:(NSInteger)部分)。这与objective-c关系不大,因为三元运算符是纯c运算符。您可以在任何C文章中找到有关它及其使用的一些信息