I have been posting updates of my utils package lately but haven’t had the time to walk through how to use the API’s in detail other than the provided ASDoc.
One of the really useful API’s in the utils package is the Iterator implementation. The Iterator API provides a standard implementation of the Iterator pattern and is useful for traversing an object, array or collection of objects. The Iterator API provides an Array Iterator, ArrayCollection Iterator and an Object Iterator. You can use them individually as needed but one of the coolest features is the Iterator Factory. The Iterator Factory handles iterator instantiation and allows a single iterator instance, typed as an IIterator interface, to use any of the iterators, via the Iterator Factory.
You can click here to view an example of how to use the Iterator Factory.
Hey Eric,
This is cool. Coming from flash we have always used for loops. I’ve just had a discussion with the java guys here over why we should be using iterators over for loops although a lot of their reasoning came down to multithreading issues.
What is the main benefit on our platform of using iterators over for loops?
Regards,
Bjorn
Hey Bjorn,
My main reason for using iterators over for loops in ActionScript is simplicity and re-usability. The Iterator pattern provides a standard mechanism for accessing collections of objects and re-usability is the key here, not necessarily performance.
When I need to loop over a collection, array etc. I can do so simply by typing an single instance of an iterator as an IIterator and use the IteratorFactory to get the type of iterator needed. I typically use Iterators more often than for loops.
Using an iterator you can also reduce your code as a while loop is much leaner than a for loop.
So again, it is a matter of personal preference.
Hope that answers your question,
Eric
Makes sense, i’m already starting to implement it now. see how i go 🙂