Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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 使用IndexPath进行NSMutableArray检查_Ios_Objective C_Nsmutablearray - Fatal编程技术网

Ios 使用IndexPath进行NSMutableArray检查

Ios 使用IndexPath进行NSMutableArray检查,ios,objective-c,nsmutablearray,Ios,Objective C,Nsmutablearray,我将UICollectionView中的触摸项目存储到NSMutableArray中,然后重新加载数据 我的问题是,如何检查IndexPath.row是否等于任何NSMutableArray对象 我可以这样做: if(indexPath.row == [myMutableArray objectAtIndex:0]) 但在本例中,它将只检查NSMutableArray中的第一个对象,我想检查所有可用对象,如果它们中有任何一个是==to indexPath.row 我是个初学者,正在努力弄明白

我将UICollectionView中的触摸项目存储到NSMutableArray中,然后重新加载数据

我的问题是,如何检查IndexPath.row是否等于任何NSMutableArray对象

我可以这样做:

if(indexPath.row == [myMutableArray objectAtIndex:0])
但在本例中,它将只检查NSMutableArray中的第一个对象,我想检查所有可用对象,如果它们中有任何一个是==to indexPath.row

我是个初学者,正在努力弄明白


谢谢。

我假设您在
myMutableArray
中正确地将值存储为
NSNumber
s

如果需要知道myMutableArray中的位置,请使用
索引对象:

NSUInteger position = [myMutableArray indexOfObject:@(indexPath.row)];
if (position != NSNotFound) {
    // handle object appearing in the array
}
if ([myMutableArray containsObject:@(indexPath.row)]) {
    // handle object appearing in the array
}
如果您只需要知道对象是否出现在数组中,请使用
containsObject:

NSUInteger position = [myMutableArray indexOfObject:@(indexPath.row)];
if (position != NSNotFound) {
    // handle object appearing in the array
}
if ([myMutableArray containsObject:@(indexPath.row)]) {
    // handle object appearing in the array
}
试试这个:

NSNumber *num=[NSNumber numberWithInteger:indexPath.row];
NSInteger anIndex=[myMutableArray indexOfObject:num];
if(NSNotFound == anIndex) {
    NSLog(@"not found");
}else {
    // anIndex contains the index. Do whatever you want.
}
希望这有帮助:)

for(int i=0;i<[myMutableArray count];i++) {


}

要想弄清楚,请阅读文件(在NSArray上)。考虑像
indexOfObject:
这样的方法。或者
indexesOfObjectsPassingTest:
。如何将行存储到数组中?您不能只添加它,因为它不是对象。您应该将行整数包装在NSNumber中。indexPath.row不是对象,并且不能在数组中出现。@vikingosegundo>>请参见我的编辑。:)是的,我看到了。更好,但实际上你为什么首先发布了Brigham答案的副本?indexPath.row不是一个对象,因此不能在数组中出现。有趣的是,这个答案被排除了,因为它显然不起作用。lol,这个答案应该被排除,而不是被接受。不管这意味着什么…这是一个简单的解决办法。我将
indexPath.row
的用法替换为
@(indexPath.row)