Objective c 为什么假设目标C for循环的行为类似于C for循环是不安全的?
我有一个称为索引的属性。我有一个可变数组,我称之为数组。我震惊地发现这段代码抛出了一个索引越界异常Objective c 为什么假设目标C for循环的行为类似于C for循环是不安全的?,objective-c,ios,loops,Objective C,Ios,Loops,我有一个称为索引的属性。我有一个可变数组,我称之为数组。我震惊地发现这段代码抛出了一个索引越界异常 if(index >= [array count]) return; for(self.item = [array objectAtIndex:index]; index < [array count]; self.item = [array objectAtIndex:index]) { index++; //do stuffs } 我预
if(index >= [array count]) return;
for(self.item = [array objectAtIndex:index]; index < [array count]; self.item = [array objectAtIndex:index]) {
index++;
//do stuffs
}
我预计会出现以下顺序:
谢谢 在第一种情况下,您是否在for循环中递增
索引
for(self.item = [array objectAtIndex:index]; index < [array count]; self.item = [array objectAtIndex:index]) {
//do stuffs
index++;
}
第一次通过时,您将首先检查条件,但随后递增器将在循环的每个迭代结束时运行。在这一点上,它将进入递增->条件->循环体
这相当于以下内容:
initialize;
while(condition) {
// do stuffs
index++;
LOOP_INCREMENTER; // this is your assignment statement, self.item = [array objectAtIndex:index];
}
1. initialization instructions
2. test condition, if condition is true do the following:
2a. execute loop body
2b. execute next iteration instruction
2c. goto step 2
在第一种情况下,是否在for循环中递增
索引
for(self.item = [array objectAtIndex:index]; index < [array count]; self.item = [array objectAtIndex:index]) {
//do stuffs
index++;
}
第一次通过时,您将首先检查条件,但随后递增器将在循环的每个迭代结束时运行。在这一点上,它将进入递增->条件->循环体
这相当于以下内容:
initialize;
while(condition) {
// do stuffs
index++;
LOOP_INCREMENTER; // this is your assignment statement, self.item = [array objectAtIndex:index];
}
1. initialization instructions
2. test condition, if condition is true do the following:
2a. execute loop body
2b. execute next iteration instruction
2c. goto step 2
您对
for
循环工作方式的期望是不正确的
for(initialization instructions; condition; next iteration instruction) {...}
顺序如下:
initialize;
while(condition) {
// do stuffs
index++;
LOOP_INCREMENTER; // this is your assignment statement, self.item = [array objectAtIndex:index];
}
1. initialization instructions
2. test condition, if condition is true do the following:
2a. execute loop body
2b. execute next iteration instruction
2c. goto step 2
所以问题是,当索引==([array count]-1)
时,循环体执行计算索引+
,设置索引==[array count]
循环体完成后,“下一个迭代指令”将尝试计算[array objectAtIndex:index]
,其中索引
太大了一倍。请记住,此步骤发生在执行循环体之后和测试条件之前。这就是C处理循环的方式
请尝试以下循环:
for(; index < [array count]; ++index) {
self.item = [array objectAtIndex:index];
//do stuffs
}
(;索引<[数组计数];++索引)的{
self.item=[array objectAtIndex:index];
//做事
}
当然,您可能还需要调整循环体的
//do stuff
区域中的索引的任何现有用法。您对循环如何工作的期望是不正确的
for(initialization instructions; condition; next iteration instruction) {...}
顺序如下:
initialize;
while(condition) {
// do stuffs
index++;
LOOP_INCREMENTER; // this is your assignment statement, self.item = [array objectAtIndex:index];
}
1. initialization instructions
2. test condition, if condition is true do the following:
2a. execute loop body
2b. execute next iteration instruction
2c. goto step 2
所以问题是,当索引==([array count]-1)
时,循环体执行计算索引+
,设置索引==[array count]
循环体完成后,“下一个迭代指令”将尝试计算[array objectAtIndex:index]
,其中索引
太大了一倍。请记住,此步骤发生在执行循环体之后和测试条件之前。这就是C处理
循环的方式
请尝试以下循环:
for(; index < [array count]; ++index) {
self.item = [array objectAtIndex:index];
//do stuffs
}
(;索引<[数组计数];++索引)的{
self.item=[array objectAtIndex:index];
//做事
}
当然,您可能还需要调整循环体的//do stuff
区域中的index
的任何现有用法。您编写的代码并不是您所期望的
若要遍历数组,则应考虑循环中的for:
for (MyObject* obj in array) {
// do something with obj
self.item = obj;
}
您正在使用的for循环在几个方面都是错误的,并且(非常)明显地)您超出了数组的限制:
在上一个有效循环中,如果增加索引,则for指令将执行for的第三个参数:
self.item = [array objectAtIndex:index]
此时,索引大于数组边界,因为条件(for指令的第二个参数)将在之后执行。您编写的代码与预期不符
若要遍历数组,则应考虑循环中的for:
for (MyObject* obj in array) {
// do something with obj
self.item = obj;
}
您正在使用的for循环在几个方面都是错误的,并且(非常)明显地)您超出了数组的限制:
在上一个有效循环中,如果增加索引,则for指令将执行for的第三个参数:
self.item = [array objectAtIndex:index]
此时,索引大于数组边界,因为条件(for指令的第二个参数)将在之后执行。在第一种情况下,如何增加索引
?如果我说得太多,我很抱歉。。。我对你的代码做了很多我不应该做的假设。@Costique我已经实现了第一个/下一个迭代器,因为不是所有的项都可以在一个批中考虑。索引首先初始化为0。这样做之后,First会调用next。您看到的代码是next的框架。这确保了在调用objectAtIndex之前有一个有效的索引。我现在太累了,无法正确地跟踪for循环的详细信息,但您预期的顺序肯定是不正确的。在循环中的代码运行之前,在Objective-C或C中测试条件。另外,请注意,Objective-C for循环就是C for循环。他们的表现是一样的。你的假设在某种程度上是错误的。在第一种情况下,你如何增加索引
。。。我对你的代码做了很多我不应该做的假设。@Costique我已经实现了第一个/下一个迭代器,因为不是所有的项都可以在一个批中考虑。索引首先初始化为0。这样做之后,First会调用next。您看到的代码是next的框架。这确保了在调用objectAtIndex之前有一个有效的索引。我现在太累了,无法正确地跟踪for循环的详细信息,但您预期的顺序肯定是不正确的。在循环中的代码运行之前,在Objective-C或C中测试条件。另外,请注意,Objective-C for循环就是C for循环。他们的表现是一样的。你的假设是错误的