循环执行无限次
你能告诉我错误可能是什么吗?
for (int r=[list count]-1; r>=0;r--) {
NSMutableArray *temp;
temp=[list objectAtIndex:r];
[list insertObject:temp atIndex:r++];
}
第一次调用时,[列表计数] 为 2。
Could you please tell me what the error could be?
for (int r=[list count]-1; r>=0;r--) {
NSMutableArray *temp;
temp=[list objectAtIndex:r];
[list insertObject:temp atIndex:r++];
}
At the first call, [list count] is 2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在循环内递增循环变量
r
,因此它永远不会递减到零。您需要将此行:更改
为:
You're incrementing your loop variable
r
inside the loop, so it can never decrement to zero.You need to change this line:
to perhaps:
您有一个无限循环,因为循环开始通过测试 (r == 1 && 1 >= 0) 并且从那时起, r 永远不会改变。您只需在 r (1) 处获取对象并将其插入到 r (1) 处,然后递增 r (r == 2)。最后,循环结束, r 递减 (r == 1) 并且您再次运行测试 (1 >= 0 ),因此它运行循环并发生完全相同的事情。
您可能想在下一个索引 (r + 1) 处插入 temp,但这会导致崩溃,因为您的数组只有 2 个位置。您需要执行 addObject: 来增加数组的大小(并在本例中将项目插入到 r + 1 处)。
You have an infinite loop because the loop starts passing the test (r == 1 && 1 >= 0) and from that point, r never changes. You simply grab the object at r (1) and insert it at r (1) and then increment r (r == 2). Finally, the loop ends, r gets decremented (r == 1) and you run your test again (1 >= 0 ) so it runs the loop and the exact same thing happens.
You probably want to insert temp at the next index (r + 1) but that will cause a crash since your array has only 2 places. You would need to do an addObject: in order to increase the size of the array (and to insert the item at r + 1 in this case).
保罗是对的。也许您认为
r++
是r+1
的简写?不是,它的意思是r=r+1
。Paul’s right. Maybe you think that
r++
is a shorthand forr+1
? It’s not, it meansr=r+1
.