虽然循环没有停止给定的标准

时间:2012-02-28 23:56:20

标签: iphone objective-c while-loop

我对这个while循环有点问题。我一直在崩溃,因为它继续执行循环,即使它已找到对象并据说增加了int。 任何想法我都有类似的方式设置了很多其他循环,它们都运行良好。

int possible = 0;

while (possible < [possibleAthetes count]) {
    if ([[[possibleAthetes objectAtIndex:possible]valueForKey:@"ID"]intValue] == [self.athleteID intValue]) {        
        [possibleAthetes removeObjectAtIndex:possible];
        possible = [possibleAthetes count] ;
    }
    possible ++;
}

1 个答案:

答案 0 :(得分:1)

即使删除对象,也可以进行递增。打破循环。

如果您需要摆脱多名运动员,请忽略休息(由于被移除的物体,不需要增加)。

int possible = 0;

while (possible < [possibleAthetes count]) {

    BOOL criteriaMatch = ([[[possibleAthetes objectAtIndex:possible]valueForKey:@"ID"]intValue] == [self.athleteID intValue]);

    if  (criteriaMatch){        
        [possibleAthetes removeObjectAtIndex:possible];
        break;
    } else {
        possible++;
    }
}