Adding pairs

Bob wants to join the game.

Take a look at his info before you let him join:

var bobData = ["name": "Bob", "profession": "Card Player", "country": "USA"]

This dictionary is of type [String: String], and it’s mutable because it’s assigned to a variable. Let’s say you got more information about Bob and you wanted to add it to the dictionary. This is how you’d do it:

bobData.updateValue("CA", forKey: "state")

There’s even a shorter way to add pairs, using subscripting:

bobData["city"] = "San Francisco"

Bob’s a professional card player. So far, he sounds like a good addition to your roster.

results matching ""

    No results matching ""