iphone - Objective c "for each" (fast enumeration) -- evaluation of collection? -


it seems experimentation collection expression evaluated once. consider example:

static nsarray *a;  - (nsarray *)fcn {     if (a == nil)         = [nsarray arraywithobjects:@"one", @"two", @"three", nil];     nslog(@"called");     return a; }  ...  (nsstring *s in [self fcn])     nslog(@"%@", s); 

the output is:

2010-10-07 07:37:31.419 widephotoviewer lite[23694:207] called 2010-10-07 07:37:31.420 widephotoviewer lite[23694:207] 1 2010-10-07 07:37:31.425 widephotoviewer lite[23694:207] 2 2010-10-07 07:37:31.425 widephotoviewer lite[23694:207] 3 

indicating [self fcn] called once.

can confirm specified (as opposed merely observed) behavior?

what have in mind doing this:

for (uiview *v in [innerview subviews]) { 

instead of this:

nsarray *vs = [innerview subviews]; (uiview *v in vs) { 

thoughts?

this kind of loop called "fast enumeration" (look @ nsfastenumeration object). apple's documentation says in "for obj in expression", expression yields object conforms nsfastenumeration protocol, guess that's correct behaviour: function called once, iterator created once , used in loop.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -