For example, the following is semantically identical to the declaration above but it uses this short-hand syntax instead: var filmCast2 : In this short-hand syntax, we use a pair of square brackets and write the two types, (one for the keys and one for the values) between the square brackets separating them with a colon. In Swift, we have an alternative, short-hand syntax that we can use to specify the types of keys and values a dictionary will hold. Now, if all these angled brackets and talk of generics is a bit much for you there is some good news. Collectively, the Dictionary type, the angled brackets and the type parameters make up the complete type so in this case the filmCast1 variable is actually of type Dictionary. In this case, the Dictionary type (our generic), accepts two other types ( String and Double) as parameters separated by a comma and specified between a pair of angled brackets. I don’t want to go into generics just now but if you’ve never come across them, just think of them as a type that accepts one or more other types as parameters. Under the hood dictionaries in Swift are actually an example of a generic. In the case of dictionaries the variable type is the interesting bit. Like any other variable we use the var keyword to declare the variable (as a result, the dictionary will be mutable) followed by the name of the variable, a colon and then the variables type. Firstly, if you’re feeling particularly formal, there is the full declaration: var filmCast1 : Dictionary For this I’ve chosen to dive into the land of Star Wars and initially create a dictionary that holds the heights of each of the characters in the original Star Wars films (yeah I know, slightly strange but go with it for now).įor each character, we’ll look up the characters height using their name as the key (a String) and store their height as the corresponding value (a Double).Īs with a lot of things in Swift, there are a number of ways we could declare this dictionary. How to Declare a Dictionaryīefore we start, we’re going to need some sort of example. Given this, let’s look at how we actually declare one. declare it with using the var keyword), you can mutate the dictionary adding, removing or modifying the contents of the dictionary to your hearts content. If however you assign the dictionary to a variable (i.e. declare it using the let keyword), the dictionary is immutable and both its size and contents cannot be changed. If you assign the dictionary to a constant (i.e. Dictionary MutabilityĪs with most other constants and variables in Swift, the mutability of a dictionary (the ability to change or mutate it’s contents) is governed by whether you assign the dictionary to a constant or variable. We’ve not covered protocols yet but essentially this means that any type you select for the key, must provide an Int property called hashValue that can be used to represent the key. All the keys in a dictionary must be of the same type and similarly, all the values must also be of the same type (though not necessarily the same type as the keys).īeyond this rule, the only caveat we have when declaring dictionaries is that the keys in a dictionary must be unique and must conform to Swifts Hashable protocol. This means that we have to specify individual types for both the keys and the values. In Swift, dictionaries are unordered collections that store multiple key-value pairs with each value being accessed via a corresponding unique key.Īs with the other Swift collection types, dictionaries in Swift are strongly typed. Iterating Over a Dictionary in a Particular Order.Iterating Over a Dictionaries Keys and Values.Adding nil Values for Keys That Reference Optional Values.Dictionaries Keys That Reference Optional Values.Finding The Number of Items in a Dictionary.Counting the Number of Items in a Dictionary.Initialising Dictionaries with Initial Items.Initialising an Empty Dictionary Using Dictionary Literals.Initialising an Empty Dictionary Using Initialiser Syntax.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |