What happens when you declare the pointer outside of the fast enumeration loop in objective c?

StackOverflow https://stackoverflow.com/questions/23368839

  •  12-07-2023
  •  | 
  •  

Вопрос

I have seen it done both ways but I am not really sure what the difference is. Here is both scenarios:

Outside of fast enumeration loop:

NSDate *date;
for(date in array)
{
    //
}

Inside of fast enumeration loop:

for(NSDate *date in array)
{
    //
}

If I had to guess, the 2nd scenario would be more expensive on memory if it is creating a new NSDate pointer for every iteration of the loop? Or is that not what is happening?

Это было полезно?

Решение

There should be no difference in terms of cost, it's just a difference in scope.

With the pointer defined outside the loop it will continue to be defined after the loop has run (and would contain a pointer to the last item enumerated if you break out of the loop) so you could reuse it for something else.

With the pointer inside the loop, the pointer is not accessible outside the loop.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top