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,对。这就是我的回答:)