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