Reputation: 157
In what way and in which cases we have to use the NSFastEnumeration in iphone can anyone tell me the sample codes to how to use these fast enumeration in iphone
Upvotes: 1
Views: 110
Reputation: 69499
NSFastEnumeration
is here to speed op loops.
So dont use:
for (in i =0; i < [myArray count]; i++){
id object [myArray objectAtIndex:i];
}
but use:
for (id object in myArray)
This will make the looping thru the array much faster.
Upvotes: 0
Reputation: 5820
NSFastEnumeration
is a protocol that your classes can adopt that allows you to use the fast enumeration construct for iterating over a collection of objects managed by your class. That is you will be able to write:
for (Object * obj in MYAwesomeObject) {
//do awesome stuff here.
}
The built-in collection classes in Foundation already implement this (NSArray, NSSet, NSDictionary) which cover many many needs for collection objects. If, say, you wanted to implement a particular tree structure, you can have your class adopt NSFastEnumeration
to allow you to iterate over all the objects in the tree without having to handle traversing directly. In this case, your class has to implement -countByEnumeratingWithState:objects:count:
to conform to the protocol which returns (through reference) a C array of objects to iterate over.
Upvotes: 1