Querying dictionaries – Apple WebObjects 3.5 User Manual

Page 203

Advertising
background image

Commonly Used Dictionary Methods

203

Querying Dictionaries

– allKeys

Returns an array containing the dictionary’s keys or an empty array
if the dictionary has no entries. This method is useful for accessing
all the entries in a dictionary. For example, the following code
excerpt creates the NSArray

keys

and uses it to access the value of

each entry in the dictionary:

id index;
id keys = [dictionary allKeys];
for (index = 0; index < [keys count]; index++) {

value = [dictionary objectForKey:[keys
objectAtIndex:index]];
// Use the value

}

– allKeysForObject:

Returns an array containing all the keys corresponding to values
equivalent to a specified object. Equivalency is determined using
the

isEqual:

method. If the specified object isn’t equivalent to any of

the values in the receiver, this method returns

nil

.

– allValues:

Returns an array containing the dictionary’s values, or an empty
array if the dictionary has no entries.

Note that the array returned from

allValues

may have a different

count than the array returned from

allKeys

. An object can be in a

dictionary more than once if it corresponds to multiple keys.

– keysSortedByValueUsingSelector:

Returns an NSArray containing the dictionary’s keys such that
their corresponding values are sorted in ascending order, as
determined by a specified method. For example, the following
code excerpt creates the NSArray

keys

containing the string

“Pasta” at index 0, “Seafood” at index 1, and “Steak” at index 2:

id choices = @{"Steak" = 3; "Seafood" = 2; "Pasta" = 1};
id keys = [choices sortedByValueUsingSelector:@"compare:"];

– count

Returns the number of entries currently in the dictionary.

Advertising