简体   繁体   English

Objective-C中FOR循环的奇怪行为

[英]Strange behavior of a FOR loop in objective-C

Found a situation for which I can not find an explanation. 找到了我找不到解释的情况。

I have an array of arrays in objective-c. 我在Objective-C中有一个数组数组。 this situation the array has 4 empty arrays. 这种情况下,数组有4个空数组。

NSArray *dados = [[NSArray alloc] initWithObjects:
             [[NSArray alloc] init],
             [[NSArray alloc] init],
             [[NSArray alloc] init],
             [[NSArray alloc] init],
             nil];

I want a "for" to go through an internal array if it has two or more numbers. 我希望“ for”通过内部数组(如果有两个或多个数字)。

for(int i=0; i<((NSArray *)[dados objectAtIndex:1]).count-1;i++){
        NSLog(@"do anything");
    }

this "for" enter into infinite loop. 这个“ for”进入无限循环。
However if I use a "NSLog" tells me that the condition is -1, and the "is" not supposed to happen 但是,如果我使用“ NSLog”告诉我条件为-1,则“ is”不应该发生

NSLog(@"%d",((NSArray *)[dados objectAtIndex:1]).count-1);

2014-02-20 15:37:42.563 iKL Time-sheet[31666:a0b] -1

how is it possible that this "for" becomes an infinite loop when the stop condition is 0 <-1? 当停止条件为0 <-1时,该“ for”怎么可能成为无限循环?

thought to be a curious case, if someone can explain what is going to be able to better understand the language I thanked 被认为是一个很好的例子,如果有人可以解释什么将能够更好地理解我所感谢的语言

---------------------//---------------- --------------------- // ----------------
answer: 回答:

for(int i=0; i<(int)(((NSArray *)[dados objectAtIndex:1]).count-1);i++){
            NSLog(@"do anything");
        }

with this cast works well, the explanation for this lies in the answer below 与此演员一起运作良好,对此的解释在于下面的答案

count is an unsigned integer ( NSUInteger ). count是一个无符号整数( NSUInteger )。 When the count is zero, subtracting one wraps, producing a large number. 当计数为零时,减去一圈就产生一个大数。 Zero is less than a large number. 零小于大数。

your code is not obvious i would re-write with fast iteration 您的代码不明显,我会通过快速迭代进行重写

NSArray *dados = [[NSArray alloc] initWithObjects:
         [[NSArray alloc] init],
         [[NSArray alloc] init],
         [[NSArray alloc] init],
         [[NSArray alloc] init],
         nil];

for(NSArray * a in dados)
{
    if([a count] >1)
    {
        NSLog(@"do something");
    }
}

edit: 编辑:

or 要么

@synchronized(dados){

    for(int i=0; i<[dados count]);i++){
       if([[dados objectAtIndex:i]count]>1)
       NSLog(@"do anything");
    }
}

because you for loop wont make it to the end... you are combining two tests that logically cant be combined... unless your array was sorted by number of elements first. 因为您的for循环不会进行到最后...您正在组合两个在逻辑上无法组合的测试...除非您的数组首先按元素数量排序。

Remove the -1 from count, otherwise even if you array has many objects it will always skip the last element . 从count中删除-1,否则,即使数组中包含许多对象,它也始终会跳过最后一个元素

for(int i=0; i<((NSArray *)[dados objectAtIndex:1]).count-1;i++){
        NSLog(@"do anything");
}
NSArray *dados = @[@[],@[],@[]];

// I want a "for" to go through an internal array
// if it has two or more numbers (skipping the last one)
for( NSUInteger idx = 0;
     (dados[1].count >= 2) && (idx < dados[1].count - 1);
     idx++) 
{
    NSLog(@"do anything");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM