01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

86 CHAPTER 4 Storing data in collections<br />

Finally, it’s possible to empty the dictionary completely by calling the removeAllObjects<br />

method, which, as its name suggests, deletes every key/value pair from the dictionary:<br />

[myDetails removeAllObjects];<br />

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

This code snippet results in the string "There are 0 details in the dictionary"<br />

because the call to removeAllObjects emptied it completely.<br />

One message that can easily be confused with setObject:forKey: is the message<br />

setValue:forKey:. Although these two messages have similar names, their behavior is<br />

subtly different with regard to how they handle the special value nil, which, as you<br />

may recall, typically represents the absence of a value.<br />

As the following code snippet demonstrates, both setObject:forKey: and set-<br />

Value:forKey: can be used to update the value stored in a dictionary for a particular key:<br />

[myDetails setObject:@"Australian" forKey:@"Nationality"];<br />

[myDetails setValue:@"Melbourne" forKey:@"City"];<br />

These two messages diverge in how they handle a nil value being specified for the<br />

key’s value. Sending setObject:forKey: with the object parameter specified as nil<br />

results in an exception that crashes your application because nil isn’t a valid value to<br />

store in a dictionary. Passing nil to setValue:forObject: is acceptable, however,<br />

because it is interpreted as though you had instead called<br />

[myDetails removeObjectForKey:@"Nationality"];<br />

Although it hardly makes a difference in the example code snippets (and arguably<br />

makes the code harder to understand), the benefits of using setValue:forKey: to<br />

remove a key/value pair really come to light when cleaning up a code snippet such as<br />

the following:<br />

NSString *myNewValue = ...get new value from somewhere...<br />

if (myNewValue == nil)<br />

[myDetails removeObjectForKey:@"Nationality"];<br />

else<br />

[myDetails setObject: myNewValue forKey:@"Nationality"];<br />

With setValue:forKey:, this entire code snippet can be replaced with a single line of<br />

source code. setValue:forKey: will be discussed in greater detail in chapter 11,<br />

which introduces the concept of Key-Value Coding. Throughout the rest of the book,<br />

we come back to the subject of dictionaries, but let’s round off this discussion with a<br />

look at how to list all entries in a dictionary, similar to an index or table of contents in<br />

a book.<br />

4.2.4 Enumerating all keys and values<br />

Like arrays, dictionaries can be enumerated to list all of the key/value pairs contained<br />

in them. But because dictionaries don’t retain any ordering, the order in which key/<br />

value pairs are enumerated may not match the sequence in which they were added to<br />

the dictionary.

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

Saved successfully!

Ooh no, something went wrong!