将“for ... in ... break”循环转换为“while”循环

时间:2011-08-31 10:10:18

标签: iphone ios computer-science

我可以在while循环中转换此循环吗?

int i=0;
for (NSDictionnary *crossArrayDictionnary in (NSArray *)mainArray) {
    //some code...
    i++;
    if (i>=50) {
        break;
    }
}

谢谢,

4 个答案:

答案 0 :(得分:2)

这应该可以解决问题:

NSInteger max, i = 0;
max = [mainArray count] < 50 ? [mainArray count] : 50;

while (i < max) {
    NSDictionary *crossArrayDictionnary = [mainArray objectAtIndex:i];
    // You code here.   
    i++;
}

答案 1 :(得分:2)

NSEnumerator *enumerator = [mainArray objectEnumerator];
NSDictionary *crossArrayDictionnary = nil;

while ((crossArrayDictionnary = [enumerator nextObject])) {

    //some code...
    i++;
    if (i>=50) {
        break;
    }
}

答案 2 :(得分:0)

不知道objective-c,但是这样:

int i=0;
while (i < 50 && i < [mainArray count]) {
    NSDictionary *crossArrayDictionnary = [mainArray objectAtIndex:i];
    //some code...
    i++;
}

答案 3 :(得分:0)

正如rckoenes所说, 微小的变化。

NSInteger i = 0;
while (i < 50 && [itemsMutable cout] >= i) {
    NSDictionary *crossArrayDictionnary = [itemsMutable objectAtIndex:i];
    // You code here.   
    i++;
}