For (;;) vs. for (:) in Objective-C Performance and Practice

Is there a difference between for(;;)and for(:)in terms of performance in Objective-C? And what good practices to use for(;;)or for(:)?

+5
source share
1 answer

I assume that in each case you are listing a collection of objects, since only form C for(;;)allows you to list primitive types. The construct for(in)uses a protocol called NSFastEnumerationto fill the buffer with objects for use in future iterations and uses a cursor to track which object it connects to. This makes it faster than:

NSEnumerator *e = [collection objectEnumerator];
while (id o = [e nextObject]) {
  //...
}

, , :

for (NSInteger i=0; i < [collection count]; i++) {
  id o = [collection objectAtIndex: i];
  //...
}

- [*]. for(in) , , 8 ​​ .

, [collection enumerateObjectsUsingBlock: ^(id obj, int idx, BOOL *stop){/*...*/}];, . , , , .

[*] - , , ; , , .

+11

All Articles