Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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 如何检查Xcode中的对象计数_Ios_Objective C - Fatal编程技术网

Ios 如何检查Xcode中的对象计数

Ios 如何检查Xcode中的对象计数,ios,objective-c,Ios,Objective C,我下面的代码崩溃了。我可以看到filteredOther有0对象。我认为它是因为这个原因而崩溃的,但我确实是Xcode的新手 我怎样才能解决这个问题 NSString *UserID = @""; UserID = [[filteredOther objectAtIndex:selectedIndex.row] objectForKey:@"id"]; 使用NSArray的count方法。 您可以通过方法访问它,如[array count] 或者作为属性:array.count if (fil

我下面的代码崩溃了。我可以看到
filteredOther
0
对象。我认为它是因为这个原因而崩溃的,但我确实是
Xcode
的新手

我怎样才能解决这个问题

NSString *UserID = @"";
UserID = [[filteredOther objectAtIndex:selectedIndex.row] objectForKey:@"id"];

使用
NSArray
count
方法。 您可以通过方法访问它,如
[array count]
或者作为属性:
array.count

if (filteredOther) {

     if ([filteredOther count] > selectedIndex.row]) {

          UserID = [[filteredOther objectAtIndex:selectedIndex.row] objectForKey:@"id"];
     } else {
         NSLog(@"Index out of bounds");
     }

} else {

     NSLog(@"Array is nil");
}

您可以通过调用
filteredOther.count
来检查
filteredOther
计数

if (filteredOther) {

     if ([filteredOther count] > selectedIndex.row]) {

          UserID = [[filteredOther objectAtIndex:selectedIndex.row] objectForKey:@"id"];
     } else {
         NSLog(@"Index out of bounds");
     }

} else {

     NSLog(@"Array is nil");
}
不确定你到底想要达到什么,但是

if (filteredOther.count > selectedIndex.row) {
    UserID = [[filteredOther objectAtIndex:selectedIndex.row] objectForKey:@"id"];
}
我假设,您需要再次检查,您正在访问一个
NSArray
,其中包含
NSDictionary
类型的对象

当您不确定是否要访问数组索引中的对象时。您可以进行如下检查:

if (filteredOther.count > selectedIndex.row) {
    //do good stuff.
} else {
    //need to check.
}

显示错误消息。这部分
[filteredOther objectAtIndex:selectedIndex.row]
是导致崩溃的原因吗?然后我们需要更多的上下文。首先,您需要检查“filteredOther”是否为nil/是否有零值。@MGP,如果OP在使用前已经初始化了它,则无需检查
nil
。然后需要检查其中的对象数。.如果索引超出其计数-1,则它肯定会崩溃…如ArrayIndexOutOfBoundException…@MGP,对。这就是我的回答:)