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!

Dictionaries in Swift Programming Explained
Tagged on:         

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share This

Exhausted by your 9 - 5?

Would you prefer to work for half the time and earn double? The first step to your freedom is understanding how apps make money. Simply enter your email below and I will send you a free PDF describing the process.

If you're serious about improving your life then do not miss this opportunity.

You have subscribed! Please check your email to confirm this subscription and to receive your item!