GDictionary

A built-in data structure that holds key-value pairs.

Dictionary.swift:35
class GDictionary

Dictionaries are associative containers that contain values referenced by unique keys. Dictionaries will preserve the insertion order when adding new entries. In other programming languages, this data structure is often referred to as a hash map or an associative array.

You can define a dictionary by placing a comma-separated list of key: value pairs inside curly braces {}.

Creating a dictionary:

You can access a dictionary’s value by referencing its corresponding key. In the above example, points_dict["White"] will return 50. You can also write points_dict.White, which is equivalent. However, you’ll have to use the bracket syntax if the key you’re accessing the dictionary with isn’t a fixed string (such as a number or variable).

In the above code, points will be assigned the value that is paired with the appropriate color selected in my_color.

Dictionaries can contain more complex data:

To add a key to an existing dictionary, access it like an existing key and assign to it:

Finally, dictionaries can contain different types of keys and values in the same dictionary:

The keys of a dictionary can be iterated with the for keyword: