Dictionaries act as storage for key-value pairs, which you can use like a real dictionary to look values up by key name. Learn about basic dictionary syntax and how to create your first dictionary.
- [Instructor] Like arrays, dictionaries are collection types, but instead of holding single values they hold what's called key value pairs. Now all keys need to be of the same type and all values need to be of the same type. It's also important to know that dictionaries are unordered and their values are accessed through their associated keys. Now let's do the same thing we did with arrays, and initialize some empty dictionaries in a few ways.
So emptyDictionary1, we'll give this a type annotation, Dictionary left arrow, right arrow. Now in here, the key type comes first, so these keys are going to be of type Int and their values are going to be of type Int equals, open and closed brackets with a colon in the middle. You can do another variation on this emptyDictionary2 equals dictionary, left arrow, Int for the key, String for the value type, and open and closed parenthesis.
Let's go with shorthand for emptyDictionary3, we'll have this equal open and closed brackets, String : String for the key in value types, and open and closed parenthesis. Finally, emptyDictionary4 is going to have a shorthand type annotation so String : String, just have this equal open and closed brackets and a colon.
Now just like arrays and pretty much everything else in Swift, choosing which one of these is wholly a matter of your preference. The compiler does not care one way or the other. I'd recommend choosing a style early on though as this'll save you a lot of headache later down the road. Alright so we've created empty dictionaries, let's create one that actually has values. I'm going to call this blacksmithShop and we're just going to initialize this with a few key value pairs.
So you'll be able to buy a Bottle for 10, a shield for 15, and an Ocarina for 1000. Again the compiler is going to correctly infer the type String and Int of our dictionary. We can also use some methods that we've already seen before. So blacksmithSHop.count and blacksmithShop.isEmpty both work.
Now if you need to extract all the keys or values from a dictionary separately, you can do that. So let's all our keys from blacksmithShop. I'm going to create a new constant called allKeys and I'm going to access the keys property on blacksmithShop. Now here in Xcode we can see that this is going to return to us a dictionary of type String and Int. We're going to get an error here because the compiler doesn't know what we want our return type to be.
So we're going to use our explicit casting knowledge and since keys is an array of Strings, that's exactly what we're going to cast it as, an array of Strings. Now on the right hand side, you'll see that we get Ocarina, Shield, and Bottle in no particular order because dictionaries are unordered. We can do the same thing with our values, but we need to cast them as an array of integers as our values are of type Int. So here we've cast it as an array of integers, and we can access this with blacksmithShop.values.
So let's try and access a dictionary value. We're going to use subscript syntax again like we did with arrays but instead of giving an index number we're going to use a key for whatever value we want. So for instance we're going to create a new constant called shieldPrice, and we're going to get this from blacksmithShop for key Shield. And we get 15 which is correct.
Now let's say we try and access a key value pair that doesn't exist. So for instance blacksmithShop for key Sword. This is going to return nil and it's actually going to return an optional which we'll get to later, but just wanted you to know that you can actually try and query a dictionary for a key value pair that doesn't exist. And you won't get a compile error, so this is up to you to sort of fix.
The last thing we want is to iterate over a dictionary and get all of its key value pairs, exactly like we did with the for loop in the array. So for, and we need to capture the key and the value so the key is going to be the itemName and the value is going to be the itemValue in blacksmithShop open and closed brackets. Now here we can just print itemName , itemValue In our debug log, we're going to get each item and each price.
- Starting new playgrounds and projects
- Variables and constants
- Writing single and multiline comments
- Core string methods
- Working with numbers
- Working with collections
- Creating arrays
- Working with sets
- Application control flow
- Writing functions
- Basic Swift classes and structs