Working with Dictionaries
NSDictionary and NSMutableDictionary objects store collections of key-value pairs. The key-value pairs within a dictionary are called entries. Each entry consists of an object that represents the key and a second object that represents the key's value. Within a dictionary, the keys are unique. That is, no two keys in a single dictionary are equivalent.The difference between NSDictionary and NSMutableDictionary is that you can't add, modify, or remove entries from an NSDictionary's initial collection of entries. Insertion and deletion methods provided for NSMutableDictionaries are not available for NSDictionaries. Although their use is limited to managing static collections of objects, it's best to use NSDictionaries wherever possible.
You can create NSDictionaries with WebScript's @ syntax for defining constant objects. For example, the following statements create NSDictionaries:
id sizes = @{"S" = "Small"; "M" = "Medium"; "L" = "Large"; "X" = "Extra Large"}; id defaultPreferences = @{ "seatAssignment" = "Window"; "smoking" = "Non-smoking"; "aircraft" = "747"};You can also create dictionaries with creation methods. For example, if you want to create an NSDictionary that contains variables, you have to use a creation method. You can't use variables with WebScript's @ syntax. The following statement creates an NSDictionary that contains variables:
id customerPreferences = [NSDictionary dictionaryWithObjectsAndKeys: seatingPreference, @"seatAssignment", smokingPreference, @"smoking", aircraftPreference, @"aircraft", nil];The variable customerPreferences is an NSDictionary, so its initial collection of entries can't be modified. To create a dictionary that can be modified, use a creation method to create an NSMutableDictionary. For example, the following statement creates an empty NSMutableDictionary:
id dictionary = [NSMutableDictionary dictionary];The methods provided by NSDictionary and NSMutableDictionary are described in more detail in the next section, "Commonly Used Dictionary Methods."
Table of Contents Next Section