Accessing dictionary values

We use the subscript syntax to retrieve the value for a particular key. If the dictionary does not contain the key we are looking for, the dictionary will return nil; therefore, the variable returned from this lookup is an optional variable. The following example shows how to retrieve a value from a dictionary using its key in the subscript syntax:

let countries = ["US":"United States", "IN":"India","UK":"UnitedKingdom"] 
var name = countries["US"] 

In the preceding code, the variable name will contain this string: United States.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset