目标c中这两个for循环之间的差异

时间:2022-07-13 21:29:56

Ok, so I'm building an iOS game, and I feel like I have a pretty good grasp on objective-c, but one thing that caused me a ton of pain was the reference-errors I got when I used the following for-loop

好吧,所以我正在构建一个iOS游戏,我觉得我对objective-c有很好的把握,但是有一件事让我感到很痛苦,因为当我使用以下内容时,我得到的参考错误 - 循环

for (MyObject *object in nsMutableArrayOfObjects) {
    // do things with object
}

vs.

for (int i = 0; i< nsMutableArrayOfObjects.count; i++) {
    // do things with nsMutableArrayOfObjects[i];
}

I had a few of these (first loop example) running in sequence and I kept getting the EXC_BAD_ACCESS error. I also had some loop nesting, where I would put on inside the other. I'm just really curious to know what the key differences are. I'm assuming the differences have something to do with how they reference the objects in the array.

我有一些这些(第一个循环示例)按顺序运行,我不断收到EXC_BAD_ACCESS错误。我也有一些循环嵌套,我会放在另一个里面。我真的很想知道关键的区别是什么。我假设差异与它们如何引用数组中的对象有关。

3 个解决方案

#1


2  

The first example you posted uses Fast Enumeration - The preferred method to enumerate collections.
Apple provides the details in the Collections Programming Guide.

您发布的第一个示例使用快速枚举 - 枚举集合的首选方法。 Apple在“集合编程指南”中提供了详细信息。

I am not sure what caused the EXC_BAD_ACCESS in your case (sounds more like a memory management issue). But one thing to keep in mind when using fast enumeration is, that you can't mutate the collection you are enumerating.

我不确定在你的情况下是什么导致了EXC_BAD_ACCESS(听起来更像是内存管理问题)。但是,使用快速枚举时要记住的一件事是,您不能改变您枚举的集合。

#2


0  

Maybe it has something to do with memory allocation as nsMutableArrayOfObjects is not creating a new pointer in the loop for every Clock cycle, whereas your first loop allocates a new pointer in each Cycle

也许它与内存分配有关,因为nsMutableArrayOfObjects没有为每个时钟周期在循环中创建一个新指针,而你的第一个循环在每个循环中分配一个新指针

Beats a wild guess

打败了一个狂野的猜测

#3


-1  

Both of cycles are correct, we can't answer as we don't see more code... Anyway, with EXC_BAD_ACCESS you are probably accessing deallocated object.

这两个周期都是正确的,我们无法回答,因为我们没有看到更多代码...无论如何,使用EXC_BAD_ACCESS您可能正在访问已释放的对象。

#1


2  

The first example you posted uses Fast Enumeration - The preferred method to enumerate collections.
Apple provides the details in the Collections Programming Guide.

您发布的第一个示例使用快速枚举 - 枚举集合的首选方法。 Apple在“集合编程指南”中提供了详细信息。

I am not sure what caused the EXC_BAD_ACCESS in your case (sounds more like a memory management issue). But one thing to keep in mind when using fast enumeration is, that you can't mutate the collection you are enumerating.

我不确定在你的情况下是什么导致了EXC_BAD_ACCESS(听起来更像是内存管理问题)。但是,使用快速枚举时要记住的一件事是,您不能改变您枚举的集合。

#2


0  

Maybe it has something to do with memory allocation as nsMutableArrayOfObjects is not creating a new pointer in the loop for every Clock cycle, whereas your first loop allocates a new pointer in each Cycle

也许它与内存分配有关,因为nsMutableArrayOfObjects没有为每个时钟周期在循环中创建一个新指针,而你的第一个循环在每个循环中分配一个新指针

Beats a wild guess

打败了一个狂野的猜测

#3


-1  

Both of cycles are correct, we can't answer as we don't see more code... Anyway, with EXC_BAD_ACCESS you are probably accessing deallocated object.

这两个周期都是正确的,我们无法回答,因为我们没有看到更多代码...无论如何,使用EXC_BAD_ACCESS您可能正在访问已释放的对象。