A dictionary stores associations between keys and values in a collection with no defined ordering. Just like a word dictionary!

Create an empty dictionary that holds ‘Ints’ as the keys and ‘Strings’ as the values:

Add a key-value entry (we’re putting the Integer ‘2’ as the key and ‘two’ as the value):

Of course the values don’t have to be a single type, they can be an array too!

Assigning an array to the key of ‘2’:

Now what if you want to access these key-value pairs? First ask for the key THEN the VALUE:

The question mark is part of optionals, a topic I also cover on this site (but don’t worry about it right now). The above example grabs key ‘2’ and element ‘1’. Remember, an element at index ‘1’ is actually the second element as indexes start at zero 🙂

Therefore the above code returns:

That’s the basics of dictionaries covered!

Where to go Next

This tutorial comes straight out of The Complete iOS Developer Course. Over 40 hours long it contains everything you need to create awe inspiring apps fast!

Click here to check it out!

Dictionaries in Swift 3 Explained

Leave a Reply

Your email address will not be published. Required fields are marked *

Share This

Want this Post in Easy to Understand Video Form?

Then get the Complete iOS Developer Course. Over 50 hours of pure Swift coding goodness designed to turn you into a coding master!

One Time Offer - $9! Click to Get it!

WARNING: Once this box is closed the offer will not be shown again.

Thank you!