beautypg.com

Commonly used dictionary methods – Apple WebObjects 3.5 User Manual

Page 201

background image

Commonly Used Dictionary Methods

201

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 using 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 using 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 next.

Commonly Used Dictionary Methods

The following sections list some of the most commonly used methods of
NSDictionary and NSMutableDictionary, grouped according to function.