PAGE 1
iOS App Development
Dictionaries
_____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 2
Learning Outcomes By the end of the topic, students should be able to 1. Differentiate between an Array and a Dictionary. 2. Build a Dictionary. 3. Add an item to a Dictionary. 4. Remove an item from a Dictionary. Vocabulary Dictionary Key Value
_____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 3
Dictionaries Since arrays are indexed using an integer value, there is a set order to them. The value that’s at index 0 is always ordered before the value for index 1, index 1 before 2, etc. If you were to go through all the elements of your array, it would come back with the same objects in the same order every time. However, sometimes you don’t want to call the items in order. Sometimes you want to call the item based on a property or characteristic. This is where dictionaries come in to play. Dictionaries are a way to index values by using k eys . A key is simply a value that has a unique value. The most common key type is a String, but keys can also be integers (like in Arrays), booleans and even floating-point numbers. Below is an example of a common use of dictionaries using Strings. To store someone’s address we use several key, value pairs. The key is the “index” into the dictionary, the value is what is stored at that keys location.
_____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 4
Although there might be some implied order here, because this is typically how an address is expressed, the data structure could remember the data in any order.
Building a Dictionary var myAddress: Dictionary = [ "City" : "Chicago" , "zip" : "60602" , "Street" : "650 W. Lake Street" , "State" : "IL" ]
Before we discuss how the above code is a dictionary, let’s briefly re-review how we declare a variable. If we wanted to declare a variable as an integer, we could do the following: var numberOfPlanets : Int = 8
In this declaration, the bold text is the type identifier. We typically add this identifier in when we need to tell the program _____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 5
specifically what type the object is. When we declare dictionaries, we replace Int with Dictionary ; however, dictionaries need to have additional information to tell the compiler what type the keys and values will be. Will the keys be integers? Will the values be strings? We need to tell the computer what to expect. To do this, we add the following information: .In the example below, we are creating a dictionary whose keys are Strings and the values it holds are also Strings.
var myAddress: Dictionary = [ "City" : "Chicago" , "zip" : "60602" , "Street" : "650 W. Lake Street" , "State" : "IL" ]
Using type-Inference, this would also be valid...
var myAddress = [ "City" : "Chicago" , "zip" : "60602" , "Street" : "650 W. Lake Street" , "State" : "IL" ]
Notice that when you add items into the Dictionary, the punctuation is (key:value, key:value). Don’t forget to add quotation marks around any strings! We are declaring a dictionary that can have key,value pairs added or deleted over time because we are using the v ar keyword. However, just like Array, we must add (or initialize) items at the time we declare them to an empty Dictionary. In other words, the following is not valid: var myAddress: Dictionary<String,Int> _____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC
PAGE 6
We cannot have an undefined Dictionary! We must supply the empty initialization as shown in the previous example. var myAddress: Dictionary<String,Int> = [:]
Adding and removing elements Now let’s look at how we add elements to a dictionary that already exists. To add an element simply following the dictionary name with [key] = value . For example, if we have a myAddress dictionary which needs a “city” key value pair added we would do so as follows: myAddress[“city”] = “Chicago”
Similar to Arrays, we have a method to remove a key, value pair. We use the removeValueForKey method on the dictionary. For example: myAddress.removeValueForKey(“city”)
will remove the key/value pair we just added.
_____________________________________________________________________________________________________________ MOBILE MAKERS ACADEMY 223 W Erie, Suite 4NW, Chicago, IL 60654 www.mobilemakers.co
© 2015 Mobile Makers Academy, LLC