01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

84 CHAPTER 4 Storing data in collections<br />

Due to the use of the NSDictionary class, all of the dictionaries constructed by the<br />

code samples in this section result in immutable dictionaries, which are read-only. If<br />

you want to create a dictionary that can be modified after creation, you need to<br />

replace the class name NSDictionary with NSMutableDictionary.<br />

Now that you can construct dictionary instances, let’s proceed to determine how to<br />

query a dictionary for details about the key/value pairs it contains.<br />

4.2.2 Accessing dictionary entries<br />

The methods for interacting with a dictionary are similar to those for an array. For<br />

example, you can send the count message to determine how many entries are currently<br />

contained in the dictionary:<br />

int count = [myDetails count];<br />

NSLog(@"There are %d details in the dictionary", count);<br />

Rather than an objectAtIndex: message, which accesses an element by index position,<br />

you’re provided with a similar message called objectForKey:, which enables you<br />

to obtain the value associated with a given key:<br />

NSString *value = [myDetails objectForKey:@"Name"];<br />

NSLog(@"My name is %@", value);<br />

This statement searches the dictionary to determine if a key named "Name" exists and<br />

then returns the value associated with it. Unlike NSArray’s objectAtIndex: message,<br />

it’s not an error to provide a key to objectForKey: that doesn’t exist in the dictionary.<br />

In this case, the special value nil will be returned, indicating that the key was not<br />

found in the dictionary.<br />

One common use of dictionaries is to store related but flexible data about a<br />

given item. For example, the dictionaries you constructed over the previous couple<br />

of pages store various bits of information about a particular person, such as name,<br />

cellphone number, and location. When interacting with such dictionaries, you’ll<br />

likely want to query the value of multiple keys in quick succession. For example, if<br />

you were printing address labels, you would probably want the person’s name and<br />

location details. Although you could make multiple calls to objectForKey:, you can<br />

also perform multiple lookups in a single statement via the objectsForKeys:not-<br />

FoundMarker: message:<br />

NSArray *keys = [NSArray arrayWithObjects:@"Name", @"City", @"Age", nil];<br />

NSArray *values = [myDetails objectsForKeys:keys notFoundMarker:@"???"];<br />

NSLog(@"%@ is located in %@ and is %@ years old",<br />

[values objectAtIndex:0],<br />

[values objectAtIndex:1],<br />

[values objectAtIndex:2]);<br />

objectsForKeys:notFoundMarker: expects to be provided with an array consisting of<br />

the keys of the dictionary entries you want to query. It then returns an array with the values<br />

of those keys. If a particular key you request isn’t present in the dictionary (such

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!