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

Get your FREEapp monetisation flowchart!

Use our epic flowchart to help you find the money in your idea! If you're thinking of making an app then this is vital information!


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