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.

Dictionaries<br />

83<br />

new entries added, removed, or updated. To create an empty dictionary, you use the<br />

dictionary factory message:<br />

NSDictionary *myDetails = [NSDictionary dictionary];<br />

This message is primarily of use only with the NSMutableDictionary subclass. Otherwise,<br />

your empty dictionary will forever stay empty because it’s immutable!<br />

A message called dictionaryWithObject:forKey: enables you to create a dictionary<br />

that initially consists of a single key/value pair:<br />

NSDictionary *myDetails = [NSDictionary dictionaryWithObject:@"Christopher"<br />

forKey:@"Name"];<br />

This code creates a new dictionary containing a single entry consisting of the key<br />

"Name" with a value of "Christopher", both of which are strings. More than likely,<br />

however, you’ll want to initialize a dictionary with multiple key/value pairs. A similarly<br />

named dictionaryWithObjects:forKeys: message allows for doing so:<br />

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

NSArray *values = [NSArray arrayWithObjects:@"Christopher", @"+643123456",<br />

@"Christchurch", nil];<br />

NSDictionary *myDetails = [NSDictionary dictionaryWithObjects:values<br />

forKeys:keys];<br />

In this example, you create a new dictionary with details about a particular person.<br />

The dictionaryWithObjects:forKeys: message expects to be provided with two<br />

arrays of equal length. The first value from the keys array is matched up with the first<br />

value from the values array, and so on, to create the key/value pairs that will populate<br />

the dictionary.<br />

The creation of temporary arrays can be tiresome, especially if your only intent is<br />

to populate a dictionary and you don’t need the arrays for other purposes. Naturally,<br />

the designers of the NSDictionary class considered this scenario, and they provided a<br />

more convenient factory method to allow you to specify multiple key/value pairs without<br />

creating temporary arrays:<br />

NSDictionary *myDetails = [NSDictionary dictionaryWithObjectsAndKeys:<br />

@"Christopher", @"Name",<br />

@"+643123456", @"Cell",<br />

@"Christchurch", @"City",<br />

nil];<br />

This message expects a variable number of parameters to be provided to it. The parameters<br />

alternate between being interpreted as a value or a key and are matched up into<br />

pairs until a nil value is detected to indicate the end of the list. Other than providing an<br />

alternative way to specify the list of key/value pairs, dictionaryWithObjectsAndKeys:<br />

and dictionaryWithObjects:forKeys: perform identical functionality.<br />

A number of other factory methods are also available on the NSDictionary and<br />

NSMutableDictionary classes. For example, dictionaryWithContentsOfURL: performs<br />

a function similar to NSArray’s arrayWithContentsOfURL: and enables a dictionary<br />

to easily be populated with contents of a file located on a website.

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

Saved successfully!

Ooh no, something went wrong!