Built in Counting

One of the most useful features in Swift is the for in loop.  This makes working with lists like arrays and dictionaries a breeze compared with other languages.  One thing that may not be so obvious is how to figure out where you are currently at in your list.

As a workaround, you could declare an index variable, and add one to it each pass through the loop.  This is a frankly ugly solution, and ugly code often turns into buggy code.  Luckily, Apple has built in a simple, elegant solution by calling the enumerated() method on your list.

Calling this method allows you to keep track of the index during your loop.  All of your setup and iteration is done in a single line:

Keeping track of the index while working with an array
And that's pretty much it.  Hopefully this simple method will help you become a better programmer.  I know it's helped me!  I hope to see you again as we learn to code together.


Popular posts from this blog

Passing Information Between View Controllers

Save the Internet

Landing Your First iOS Developer Job: My Journey Changing Careers