<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://commons.oreilly.com/wiki/skins/common/feed.css?97"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Cocoa_Overview_and_Foundation/The_Cocoa_Foundation_Kit&amp;action=history&amp;feed=atom</id>
		<title>Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Cocoa_Overview_and_Foundation/The_Cocoa_Foundation_Kit&amp;action=history&amp;feed=atom"/>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Cocoa_Overview_and_Foundation/The_Cocoa_Foundation_Kit&amp;action=history"/>
		<updated>2013-05-25T18:51:16Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.11.0</generator>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Cocoa_Overview_and_Foundation/The_Cocoa_Foundation_Kit&amp;diff=6263&amp;oldid=prev</id>
		<title>Docbook2Wiki: Initial conversion from Docbook</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Cocoa_Overview_and_Foundation/The_Cocoa_Foundation_Kit&amp;diff=6263&amp;oldid=prev"/>
				<updated>2008-03-07T12:56:06Z</updated>
		
		<summary type="html">&lt;p&gt;Initial conversion from Docbook&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Learning Cocoa with Objective-C/TOC}}&lt;br /&gt;
Now that we have filled your head with lots of theory about object-oriented programming, we'll look into some of the essential parts of Cocoa's Foundation framework. In this chapter we cover strings, collections, and memory management. Once you have a firm grasp on these topics, you'll be ready for the raison d'être of Cocoa: GUI programming.&lt;br /&gt;
&lt;br /&gt;
The nature of these topics doesn't lend itself to a nifty, all-inclusive code example that shows everything in action at once. So, instead of contriving a single, awkward example, we're just going to work through a set of simple code samples to illustrate the concepts presented. We'll also augment the use of these samples with some usage of the debugger.&lt;br /&gt;
&lt;br /&gt;
== Strings ==&lt;br /&gt;
&lt;br /&gt;
So far, we have worked with strings using the &amp;lt;tt&amp;gt;@&amp;quot; . . . &amp;quot;&amp;lt;/tt&amp;gt; construct in various method and function calls. This construct is convenient when working with strings. When interpreted by the compiler, it is translated into an &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; object that is based on the 7-bit ASCII-encoded string (also known as a &amp;quot;C string&amp;quot;) between the quotes. For example, the statement:&lt;br /&gt;
&lt;br /&gt;
 NSString * lush = @'''&amp;quot;'''Lush'''&amp;quot;''';&lt;br /&gt;
&lt;br /&gt;
is functionally equivalent to:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;NSString * lush = [[NSString alloc] initWithCString:&amp;lt;/nowiki&amp;gt;'''&amp;quot;'''Lush'''&amp;quot;'''];&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; objects are not limited to the ASCII character set; they can handle any character contained in the Unicode character set, allowing most of the world's living languages to be represented. Unicode is a 16-bit-wide character set, but can be represented in 8-bits using the UTF-8 encoding.&lt;br /&gt;
&lt;br /&gt;
=== Basic String Operations ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; provides several methods that are handy when working with strings. A few of these methods are as follows:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (int)'''length'''&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns the number of Unicode characters in the string object upon which it is called.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;dl&amp;gt;&lt;br /&gt;
&amp;lt;dt&amp;gt;&amp;lt;tt&amp;gt;- (const char *)'''cString'''&amp;lt;/tt&amp;gt;&lt;br /&gt;
&amp;lt;dd&amp;gt;Returns a representation of the string as a C string in the default encoding. This method is helpful when you need to operate with C-based functions, such as those found in traditional Unix system calls.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;warning&amp;quot;&amp;gt;&lt;br /&gt;
'''Warning'''&lt;br /&gt;
&lt;br /&gt;
It's important to note that since C strings are 7-bit, and the &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; class can handle the full Unicode character set, not all &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; objects can be represented as C strings.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/dl&amp;gt;&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (const char *)&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;'''UTF8String'''&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns a representation of the string as a UTF-8 representation. UTF-8 allows the transmission of Unicode characters over channels that support 8-bit encodings. All of the lower levels of Mac OS X — including the HFS+ and UFS filesystems, as well as the BSD system routines — can handle &amp;lt;tt&amp;gt;char *&amp;lt;/tt&amp;gt; arguments in the UTF-8 encoding.&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (NSString *)'''stringByAppendingString:'''(NSString *)aString&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns a new string object by appending the given string to the string upon which the method is called.&lt;br /&gt;
&lt;br /&gt;
To explore these methods, we'll create a simple program using the following steps:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;In Project Builder, create a new Foundation Tool (File → New Project → Tool → Foundation Tool) named &amp;quot;strings&amp;quot;, and save it in your ''~/LearningCocoa''folder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
You may have noticed that sometimes our project names start with a lowercase letter and sometimes with an uppercase letter. The common practice in naming applications is that command-line applications should be lowercase and GUI applications should be initial capitalized. We'll use this practice through this book.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Open the ''main.m''file, located in the &amp;quot;Source&amp;quot; group, and modify it to match the code shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-EX-1|Example 4-1]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-4-EX-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 4-1. Working with strings'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''    NSString * artist = @&amp;quot;Underworld&amp;quot;;                               // a'''&lt;br /&gt;
 &lt;br /&gt;
                            '''    NSLog(@&amp;quot;%@ has length: %d&amp;quot;, artist, [artist length])             // b'''&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code we added in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-EX-1|Example 4-1]] performs the following tasks:&lt;br /&gt;
&lt;br /&gt;
# Declares an object of type &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt;, named &amp;lt;tt&amp;gt;artist&amp;lt;/tt&amp;gt;, and sets it to the value &amp;lt;tt&amp;gt;&amp;quot;Underworld&amp;quot;&amp;lt;/tt&amp;gt;&lt;br /&gt;
# Obtains the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; of the string and prints it using the &amp;lt;tt&amp;gt;NSLog&amp;lt;/tt&amp;gt; function&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run ([[Image:Learning Cocoa with Objective-C_I_4_tt84.png|]]-R) the application. You will be prompted to save the ''main.m''file, and then Project Builder will compile and the run the code. You should see the following output in Project Builder's console:&lt;br /&gt;
&lt;br /&gt;
 2002-06-17 23:29:32.344 strings[1147] Underworld has length: 10&lt;br /&gt;
&lt;br /&gt;
As we have seen before, the &amp;lt;tt&amp;gt;NSLog&amp;lt;/tt&amp;gt; function prints the current date and time, the program name, and the process ID (PID) of the program, as well as the output that we told it to print. In the output, we see that the &amp;lt;tt&amp;gt;artist&amp;lt;/tt&amp;gt; object was substituted for the &amp;lt;tt&amp;gt;%@&amp;lt;/tt&amp;gt; token and that the return value from the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method was substituted for the &amp;lt;tt&amp;gt;%d&amp;lt;/tt&amp;gt; token. Remember, you can use any of the standard &amp;lt;tt&amp;gt;printf&amp;lt;/tt&amp;gt; substitution tokens in the format string, in addition to the &amp;lt;tt&amp;gt;%@&amp;lt;/tt&amp;gt; token.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Setting breakpoints and debugging ====&lt;br /&gt;
&lt;br /&gt;
Instead of adding code to the strings tool, we will use the debugger to explore the &amp;lt;tt&amp;gt;UTF8String&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;stringByAppendingString&amp;lt;/tt&amp;gt; methods. This will give you some practice using the debugger, while you learn about these methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Set a breakpoint between the &amp;lt;tt&amp;gt;NSLog&amp;lt;/tt&amp;gt; function and the &amp;lt;tt&amp;gt;[pool release]&amp;lt;/tt&amp;gt; line of code in ''main.m''. Remember to set a breakpoint, click in the column on the left side of the code editor. If you want to move the breakpoint, click and drag the breakpoint to its new location. In our code, this breakpoint is at line 8. An example of the breakpoint set is shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-FIG-1|Figure 4-1]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-4-FIG-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 4-1. Setting a breakpoint in the main.m file'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_4_tt86.png|Setting a breakpoint in the main.m file]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and debug the application (Build → Build and Debug, or [[Image:Learning Cocoa with Objective-C_I_4_tt87.png|]]-Y). Execution will start and then pause at the breakpoint we set, highlighting the line at which it stopped in a salmon colored bar.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Click on the Console tab above the variable viewer to open up the debugger console, as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-FIG-2|Figure 4-2]]. You should see that the &amp;lt;tt&amp;gt;NSLog&amp;lt;/tt&amp;gt; function outputs its string.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-4-FIG-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 4-2. Program execution paused at breakpoint'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_4_tt88.png|Program execution paused at breakpoint]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
The debugger console behaves similarly to working with the Terminal application. You enter a command, hit Return, and the result of the command is shown on the next line. Just like the default shell in the Terminal, the debugger maintains a history of commands that you can access by hitting the up and down arrows on your keyboard.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Type in &amp;lt;tt&amp;gt;print-object artist&amp;lt;/tt&amp;gt; at the &amp;lt;tt&amp;gt;(gdb)&amp;lt;/tt&amp;gt; prompt in the debugger console. You may have to click in the debugger console to give it focus, so that you can enter commands.&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''print-object artist'''&lt;br /&gt;
                         &lt;br /&gt;
&lt;br /&gt;
When you enter this command, the debugger outputs the following:&lt;br /&gt;
&lt;br /&gt;
 Underworld&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to simply printing objects, we can print the result of any message that we can send to an object. This functionality is incredibly useful when trying to find the various states of an object while using the debugger.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Enter in the following into the debugger console:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''print-object [artist description]'''&lt;br /&gt;
                         &lt;br /&gt;
&lt;br /&gt;
The following result, matching what we just saw in Step 4, will be printed:&lt;br /&gt;
&lt;br /&gt;
 Underworld&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Let's see the &amp;lt;tt&amp;gt;stringByAppendingString&amp;lt;/tt&amp;gt; method in action. Enter the following into the debugger console:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''print-object [artist stringByAppendingString:@&amp;quot;: Pearl's Girl&amp;quot;]'''&lt;br /&gt;
                         &lt;br /&gt;
&lt;br /&gt;
The debugger outputs the following result of the method call:&lt;br /&gt;
&lt;br /&gt;
 Underworld: Pearl's Girl&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;You can also send messages to &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; objects created using the &amp;lt;tt&amp;gt;@&amp;quot;...&amp;quot;&amp;lt;/tt&amp;gt; construct. Enter the following into the debugger console:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''print-object [@&amp;quot;The artist is: &amp;quot; stringByAppendingString:artist]'''&lt;br /&gt;
                         &lt;br /&gt;
&lt;br /&gt;
The debugger outputs:&lt;br /&gt;
&lt;br /&gt;
 The artist is: Underworld&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next debugger command we will learn is the &amp;lt;tt&amp;gt;print&amp;lt;/tt&amp;gt; command. This command prints out C types instead of objects. We will use the &amp;lt;tt&amp;gt;print&amp;lt;/tt&amp;gt; command to evaluate the return values of the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;UTF8String&amp;lt;/tt&amp;gt; methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Enter the following into the debugger console:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''print (int) [artist length]'''&lt;br /&gt;
                         &lt;br /&gt;
&lt;br /&gt;
The debugger outputs:&lt;br /&gt;
&lt;br /&gt;
 $1 = 10&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;$1&amp;lt;/tt&amp;gt; symbol is a temporary variable that holds the results of the message, and the &amp;lt;tt&amp;gt;10&amp;lt;/tt&amp;gt; denotes the number of characters (or &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt;) in the &amp;lt;tt&amp;gt;artist&amp;lt;/tt&amp;gt; object. Note that we needed to cast the return type from the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; message so that the &amp;lt;tt&amp;gt;print&amp;lt;/tt&amp;gt; command could operate. Try this again without the &amp;lt;tt&amp;gt;(int)&amp;lt;/tt&amp;gt; cast.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;To see the &amp;lt;tt&amp;gt;UTF8String&amp;lt;/tt&amp;gt; method in action, enter the following:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''print (char *) [artist UTF8String]'''&lt;br /&gt;
                         &lt;br /&gt;
&lt;br /&gt;
The debugger outputs something similar to the following:&lt;br /&gt;
&lt;br /&gt;
 $2 = 0x9f738 &amp;quot;Underworld\000&amp;quot;...&lt;br /&gt;
&lt;br /&gt;
This is the null-terminated &amp;lt;tt&amp;gt;char *&amp;lt;/tt&amp;gt; string representation, in UTF-8 encoding, of our &amp;lt;tt&amp;gt;artist&amp;lt;/tt&amp;gt; string.&lt;br /&gt;
&lt;br /&gt;
To quit the debugger, you can either click the stop button or enter &amp;lt;tt&amp;gt;quit&amp;lt;/tt&amp;gt; at the &amp;lt;tt&amp;gt;(gdb)&amp;lt;/tt&amp;gt; prompt.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;sidebar&amp;quot;&amp;gt;&lt;br /&gt;
'''Debugger Command Cheat Sheet'''&lt;br /&gt;
&lt;br /&gt;
There's a lot more that the debugger can do. Here are a few of our favorite debugger commands. Try them out to see what they do.&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;call [exp]&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Calls the given function on an object.&lt;br /&gt;
;&amp;lt;tt&amp;gt;print [exp]&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Prints the primitive value of the expression given.&lt;br /&gt;
;&amp;lt;tt&amp;gt;print-object [exp]&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Prints the value of the object returned by the expression.&lt;br /&gt;
;&amp;lt;tt&amp;gt;set [variable] = [exp]&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Sets the variable to the value of the expression. For example, we can reset the artist variable to a new string by using the expression &amp;lt;tt&amp;gt;set&amp;lt;/tt&amp;gt; &amp;lt;tt/&amp;gt; &amp;lt;tt&amp;gt;artist = @&amp;quot;New Artist&amp;quot;.&amp;lt;/tt&amp;gt;&lt;br /&gt;
;&amp;lt;tt&amp;gt;whatis [variable]&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Prints the kind, or type, of a variable.&lt;br /&gt;
;&amp;lt;tt&amp;gt;help&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Prints out a list of the commands available while using the debugger.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Working with Portions of a String ===&lt;br /&gt;
&lt;br /&gt;
When working with strings, it often is necessary to extract data from them. The &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; class provides the following methods for finding and obtaining substrings:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (NSRange)'''rangeOfString:'''(NSString *)aString&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns an &amp;lt;tt&amp;gt;NSRange&amp;lt;/tt&amp;gt; struct that contains the &amp;lt;tt&amp;gt;location&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; of the first occurrence of the given string&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (NSString *)'''substringFromIndex:'''(unsigned)index&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns a string object that contains the characters of the receiver, from the index given to the end of the string&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (NSString *)'''substringToIndex:'''(unsigned)index&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns a string object that contains the characters of the receiver, from the beginning of the string to the index given&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (NSString *)'''substringWithRange:'''(NSRange)range&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns a string object that contains the characters of the receiver, within the range specified&lt;br /&gt;
&lt;br /&gt;
To explore these methods, we'll create a simple program (that works with just substrings) using the following steps:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;In Project Builder, create a new Foundation Tool (File → New Project → Tool → Foundation Tool) named &amp;quot;substrings&amp;quot;, and save it in your ''~/LearningCocoa''folder.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Open the ''main.m''file, located in the &amp;quot;Source&amp;quot; group, and modify it to match the code shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-EX-2|Example 4-2]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-4-EX-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 4-2. Working with substrings'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''    NSString * song = @&amp;quot;Let Forever Be,The Chemical Brothers&amp;quot;;          // a'''&lt;br /&gt;
                            '''    NSRange range = [song rangeOfString:@ &amp;quot;,&amp;quot;];                         // b'''&lt;br /&gt;
                            '''    printf(&amp;quot;comma location: %i\n&amp;quot;, range.location);                     // c'''&lt;br /&gt;
 &lt;br /&gt;
                            '''    NSString * title = [song substringToIndex:range.location];          // d'''&lt;br /&gt;
                            '''    NSString * artist =''' &lt;br /&gt;
                            '''        [song substringFromIndex:range.location + range.length];        // e'''&lt;br /&gt;
 &lt;br /&gt;
                            '''    printf(&amp;quot;title:  %s\n&amp;quot;, [title UTF8String]);                         // f'''&lt;br /&gt;
                            '''    printf(&amp;quot;artist: %s\n&amp;quot;, [artist UTF8String]);                        // g'''&lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code we added in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-EX-2|Example 4-2]] performs the following tasks:&lt;br /&gt;
&lt;br /&gt;
# Declares a string object named &amp;lt;tt&amp;gt;song&amp;lt;/tt&amp;gt; and sets it.&lt;br /&gt;
# Obtains the range of the comma in the &amp;lt;tt&amp;gt;song&amp;lt;/tt&amp;gt; string.&lt;br /&gt;
# Prints the location of the comma. Notice that we are using the standard C &amp;lt;tt&amp;gt;printf&amp;lt;/tt&amp;gt; function here. We will use &amp;lt;tt&amp;gt;printf&amp;lt;/tt&amp;gt; instead of &amp;lt;tt&amp;gt;NSLog&amp;lt;/tt&amp;gt; in many of the upcoming exercises, so the output from our programs won't be cluttered with timestamps and PIDs. Note that, unlike the &amp;lt;tt&amp;gt;NSLog&amp;lt;/tt&amp;gt; function, we have to be sure to include the &amp;lt;tt&amp;gt;\n&amp;lt;/tt&amp;gt; character to print out the new line.&lt;br /&gt;
# Declares a string named &amp;lt;tt&amp;gt;title&amp;lt;/tt&amp;gt; and sets it to the substring, from the start of the &amp;lt;tt&amp;gt;song&amp;lt;/tt&amp;gt; string to the location of the comma.&lt;br /&gt;
# Declares a string named artist and sets it to the substring, from the comma to the end of the &amp;lt;tt&amp;gt;song&amp;lt;/tt&amp;gt; string. We use the &amp;lt;tt&amp;gt;range.location&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;+&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;range.length&amp;lt;/tt&amp;gt; construction so that we find the index just after the comma value. If we just used the location of the comma, it would show up in our substring.&lt;br /&gt;
# Prints the &amp;lt;tt&amp;gt;title&amp;lt;/tt&amp;gt; to the console, using the UTF-8 representation of the string. Notice that we are using the &amp;lt;tt&amp;gt;printf %s&amp;lt;/tt&amp;gt; token.&lt;br /&gt;
# Prints the &amp;lt;tt&amp;gt;artist&amp;lt;/tt&amp;gt; to the console, using the UTF-8 representation of the string.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run ([[Image:Learning Cocoa with Objective-C_I_4_tt101.png|]]-R) the application. You will be prompted to save your files, and then Project Builder will compile and run the code. You should see the following output in the console:&lt;br /&gt;
&lt;br /&gt;
 comma location: 14&lt;br /&gt;
 title:  Let Forever Be&lt;br /&gt;
 artist: The Chemical Brothers&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mutable Strings ===&lt;br /&gt;
&lt;br /&gt;
Once created, instances of the &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; class cannot be changed; they are ''immutable''. If you want to change the contents of an &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; object, you must create a new one, as we saw using the &amp;lt;tt&amp;gt;stringByAppendingString&amp;lt;/tt&amp;gt; method. In programs that manipulate strings extensively, this would become cumbersome quickly. To let you modify the contents of a string, Cocoa provides the &amp;lt;tt&amp;gt;NSMutableString&amp;lt;/tt&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
If you have programmed in Java, &amp;lt;tt&amp;gt;NSMutableString&amp;lt;/tt&amp;gt; can be considered analogous to the &amp;lt;tt&amp;gt;java.lang.StringBuffer&amp;lt;/tt&amp;gt; class.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some of the methods that you frequently will use with mutable strings are the following:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (void)'''appendString:'''(NSString *)aString&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Adds the characters of the given string to those already in the mutable string object upon which the method is called.&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (void)'''deleteCharactersInRange:'''(NSRange)range&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Deletes the characters in a given range.&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (void)&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;'''insertString:'''&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;(NSString *)aString '''atIndex:'''(unsigned index)&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Inserts the characters of the given string into the mutable string at the location specified by the index. All of the characters from the insertion point to the end of the mutable string are shifted to accommodate the new characters.&lt;br /&gt;
&lt;br /&gt;
To explore these methods, we'll create yet another simple program, using the following steps:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;In Project Builder, create a new Foundation Tool (File → New Project → Tool → Foundation Tool) named &amp;quot;mutablestrings&amp;quot;, and save it in your ''~/LearningCocoa''folder.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Open the ''main.m''file, located in the &amp;quot;Source&amp;quot; group, and modify it to match the following code:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''&amp;lt;nowiki&amp;gt;    NSMutableString * song = [[NSMutableString alloc] init];           // a&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
                         '''    [song appendString:@&amp;quot;Deaf Leppard&amp;quot;];                               // b'''&lt;br /&gt;
                         '''    printf(&amp;quot;%s\n&amp;quot;, [song UTF8String]);                                 // c'''&lt;br /&gt;
 &lt;br /&gt;
                         '''    NSRange range = [song rangeOfString:@&amp;quot;Deaf&amp;quot;];                      // d'''&lt;br /&gt;
                         '''    [song replaceCharactersInRange:range withString:@&amp;quot;Def&amp;quot;];           // e'''&lt;br /&gt;
                         '''    printf(&amp;quot;%s\n&amp;quot;, [song UTF8String]);                                 // f'''&lt;br /&gt;
 &lt;br /&gt;
                         '''    [song insertString:@&amp;quot;Animal by &amp;quot; atIndex:0];                       // g'''&lt;br /&gt;
                         '''    printf(&amp;quot;%s\n&amp;quot;, [song UTF8String]);                                 // h'''&lt;br /&gt;
 &lt;br /&gt;
                         '''    [song release];                                                    // i'''&lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The code we added performs the following tasks:&lt;br /&gt;
&lt;br /&gt;
# Creates a new empty mutable string named &amp;lt;tt&amp;gt;song&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# Appends the contents of the &amp;quot;Deaf Leppard&amp;quot; string to the &amp;lt;tt&amp;gt;song&amp;lt;/tt&amp;gt; mutable string.&lt;br /&gt;
# Prints the &amp;lt;tt&amp;gt;song&amp;lt;/tt&amp;gt; mutable string to the console.&lt;br /&gt;
# Gets the range of the &amp;quot;Deaf&amp;quot; substring.&lt;br /&gt;
# Replaces the &amp;quot;Deaf&amp;quot; substring with &amp;quot;Def&amp;quot; to correct the misspelling.&lt;br /&gt;
# Prints the &amp;lt;tt&amp;gt;song&amp;lt;/tt&amp;gt; mutable string to the console.&lt;br /&gt;
# Inserts the string &amp;quot;Animal by&amp;quot; at the beginning the mutable string.&lt;br /&gt;
# Once again prints the &amp;lt;tt&amp;gt;song&amp;lt;/tt&amp;gt; mutable string.&lt;br /&gt;
# Releases the &amp;lt;tt&amp;gt;song&amp;lt;/tt&amp;gt; object. Because we created the &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; object using the &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; method, we are responsible forr releasing it. We'll explain more about how this works later in this chapter.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run ([[Image:Learning Cocoa with Objective-C_I_4_tt104.png|]]-R) the application. You should see the following output in the console:&lt;br /&gt;
&lt;br /&gt;
 Deaf Leppard&lt;br /&gt;
 Def Leppard&lt;br /&gt;
 Animal by Def Leppard&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;sidebar&amp;quot;&amp;gt;&lt;br /&gt;
'''Mutability Versus Immutability'''&lt;br /&gt;
&lt;br /&gt;
Why does Cocoa provide both mutable and immutable versions of strings and the collection classes? The answer is that there are tradeoffs to both kinds of objects. An immutable object, once created, can't change. This means that a higher performing implementation can be used. Mutable objects, on the other hand, aren't nearly so amenable to performance tuning, as there is overhead involved in keeping them &amp;quot;editable.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
In addition, immutable objects are inherently thread-safe and can be passed to code worked on by other programmers without fear that the contents of the object will be modified. When creating your own code, you should favor immutable objects unless you need the ability to change the contents of an object.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Working with Files ===&lt;br /&gt;
&lt;br /&gt;
A common use of strings is to work with paths to files in the filesystem. The &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; class provides several methods to manipulate strings as filesystem paths, extract a file name or an extension, resolve paths containing symbolic links, and even expand tilde expressions (such as ''~duncan/Library'') in paths. Some of the commonly used path manipulation methods are as follows:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (NSString *)'''lastPathComponent'''&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns the last path component of the receiver. For example, if you call this method on the string &amp;lt;tt&amp;gt;~/LearningCocoa/substrings/main.m&amp;lt;/tt&amp;gt;, it will return &amp;lt;tt&amp;gt;main.m&amp;lt;/tt&amp;gt;.&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (NSString *)'''pathExtension'''&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns the extension, if any, of a file path. For example, if you call this method on the string &amp;lt;tt&amp;gt;main.m&amp;lt;/tt&amp;gt;, it will return the value &amp;lt;tt&amp;gt;m&amp;lt;/tt&amp;gt;.&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (NSString *)'''stringByStandardizingPath'''&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns a string with all extraneous path components removed or resolved. This method will resolve the initial tilde expression, as well as any &amp;lt;tt&amp;gt;..&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;./&amp;lt;/tt&amp;gt; symbols, to actual directories.&lt;br /&gt;
&lt;br /&gt;
In addition to working with paths, you can also create string objects using the contents of a file and write string objects to files using the following methods:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (NSString *)'''stringWithContentsOfFile:'''(NSString *)path&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Creates a new string by reading characters from the file specified by the path argument.&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (BOOL)'''writeToFile:'''(NSString *)path '''atomically:'''(BOOL)flag&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Writes the contents of the string to the given file. The &amp;lt;tt&amp;gt;atomically&amp;lt;/tt&amp;gt; flag indicates whether the file should be written safely to an auxiliary file, then copied into place. Most of the time, this setting makes no difference. The only time it matters is if the system crashes when the file is being flushed to disk.&lt;br /&gt;
&lt;br /&gt;
To see these methods in action, follow the following steps:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;In Project Builder, create a new Foundation Tool (File → New Project → Tool → Foundation Tool) named &amp;quot;filestrings&amp;quot;, and save it in your ''~/LearningCocoa''folder.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Open the ''main.m''file, located in the &amp;quot;Source&amp;quot; group, and modify it to match the code shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-EX-3|Example 4-3]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-4-EX-3&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 4-3. Reading files into strings'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''    NSString * filename = @&amp;quot;~/LearningCocoa/filestrings/main.m&amp;quot;;        // a'''&lt;br /&gt;
 &lt;br /&gt;
                            '''    filename = [filename stringByStandardizingPath];                    // b'''&lt;br /&gt;
 &lt;br /&gt;
                            '''    printf(&amp;quot;%s\n&amp;quot;, [filename UTF8String]);                              // c'''&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
                            '''    NSString * source = [NSString stringWithContentsOfFile:filename];   // d'''&lt;br /&gt;
 &lt;br /&gt;
                            '''    printf(&amp;quot;%s\n&amp;quot;, [source UTF8String]);                                // e'''&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code we added in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-EX-3|Example 4-3]] performs the following tasks:&lt;br /&gt;
&lt;br /&gt;
# Creates a string object, named &amp;lt;tt&amp;gt;filename&amp;lt;/tt&amp;gt;, that contains the path to the ''main.m''source file of this project. Note that you must save your project in your ''~/LearningCocoa''folder for this example to work. If you are saving your projects to some other location, you will need to edit the path appropriately.&lt;br /&gt;
# Sets the &amp;lt;tt&amp;gt;filename&amp;lt;/tt&amp;gt; variable to a standardized path. This will resolve the &amp;lt;tt&amp;gt;~/&amp;lt;/tt&amp;gt; characters to your home directory.&lt;br /&gt;
# Prints the resolved &amp;lt;tt&amp;gt;filename&amp;lt;/tt&amp;gt; variable.&lt;br /&gt;
# Creates a new string, named &amp;lt;tt&amp;gt;source&amp;lt;/tt&amp;gt;, with the contents of the ''main.m''source file.&lt;br /&gt;
# Prints the &amp;lt;tt&amp;gt;source&amp;lt;/tt&amp;gt; string to the console.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run ([[Image:Learning Cocoa with Objective-C_I_4_tt106.png|]]-R) the application. You should see output similar to the following appear in the console:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;/Users/duncan/LearningCocoa/filestrings/main.m&lt;br /&gt;
#import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
&lt;br /&gt;
    NSString * filename = @&amp;quot;~/LearningCocoa/filestrings/main.m&amp;quot;;&lt;br /&gt;
    filename = [filename stringByStandardizingPath];&lt;br /&gt;
    printf(&amp;quot;%s\n&amp;quot;, [filename UTF8String]);&lt;br /&gt;
&lt;br /&gt;
    NSString * source = [NSString stringWithContentsOfFile:filename];&lt;br /&gt;
    printf(&amp;quot;%s\n&amp;quot;, [source UTF8String]);&lt;br /&gt;
&lt;br /&gt;
    [pool release];&lt;br /&gt;
    return 0;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We'll explore the &amp;lt;tt&amp;gt;lastPathComponent&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;pathExtension&amp;lt;/tt&amp;gt; methods using the debugger.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Set a breakpoint between the last &amp;lt;tt&amp;gt;printf&amp;lt;/tt&amp;gt; statement and the &amp;lt;tt&amp;gt;[pool release];&amp;lt;/tt&amp;gt; line. If you typed the code exactly as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-EX-3|Example 4-3]], the breakpoint will be on line 12.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and debug the application ([[Image:Learning Cocoa with Objective-C_I_4_tt108.png|]]-Y). Execution will start and then pause at the breakpoint.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Click on the Console tab above the variable viewer to open up the debugger console.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Type in the following at the &amp;lt;tt&amp;gt;(gdb)&amp;lt;/tt&amp;gt; prompt:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''print-object [filename''' &lt;br /&gt;
                         '''lastPathComponent]'''&lt;br /&gt;
                      &lt;br /&gt;
&lt;br /&gt;
When you enter this command, the debugger should output the following:&lt;br /&gt;
&lt;br /&gt;
 main.m&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Type in the following at the &amp;lt;tt&amp;gt;(gdb)&amp;lt;/tt&amp;gt; prompt:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''print-object [filename''' &lt;br /&gt;
                         '''pathExtension]'''&lt;br /&gt;
                      &lt;br /&gt;
&lt;br /&gt;
When you enter this command, you should see the following:&lt;br /&gt;
&lt;br /&gt;
 m&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Quit the debugger; use the Stop button, or type in &amp;lt;tt&amp;gt;quit&amp;lt;/tt&amp;gt; at the &amp;lt;tt&amp;gt;(gdb)&amp;lt;/tt&amp;gt; prompt and hit return.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now that we've covered quite a few things that you can do with strings, it's time to look at Cocoa's collection classes.&lt;br /&gt;
&lt;br /&gt;
== Collections ==&lt;br /&gt;
&lt;br /&gt;
Cocoa provides several classes in the Foundation Kit whose purpose is to hold and organize instances of other classes. These are called the ''collection classes''. There are three primary flavors of collections in Cocoa: ''arrays'' , ''sets'' , and ''dictionaries'' . These classes, shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-FIG-3|Figure 4-3]], are extremely useful in Cocoa application development, and their influence can be found throughout the Cocoa class libraries.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-4-FIG-3&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 4-3. Cocoa collection classes'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_4_tt113.png|Cocoa collection classes]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Collection classes, like strings, come in two forms: ''mutable'' and ''immutable'' . Immutable classes allow you to add items when the collection is created, but no further changes are allowed. On the other hand, mutable classes allow you to add and remove objects programmatically after the collection is created.&lt;br /&gt;
&lt;br /&gt;
Much of the power of collection classes comes from their ability to manipulate the objects they contain. Not every collection object can perform every function, but in general, collection objects can do the following:&lt;br /&gt;
&lt;br /&gt;
* Derive their initial contents from files and URLs, as well as other collections of objects&lt;br /&gt;
* Add, remove, locate, and sort contents&lt;br /&gt;
* Compare their contents with other collection objects&lt;br /&gt;
* Enumerate over their contents&lt;br /&gt;
* Send a message to the objects that they contain&lt;br /&gt;
* Archive their contents to a file on disk and retrieve it later&amp;lt;ref&amp;gt;Objects placed into an array must implement certain methods to support this functionality. All of the Foundation classes that you are likely to add to a collection are already prepared for this.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Arrays ===&lt;br /&gt;
&lt;br /&gt;
Arrays—instances of the &amp;lt;tt&amp;gt;NSArray&amp;lt;/tt&amp;gt; class—are ordered collections of objects indexed by integers. Like C-based arrays, the first object in an array is located at index 0. Unlike C- and Java-based arrays whose size is set when they are created, Cocoa mutable array objects can grow as needed to accommodate inserted objects.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;NSArray&amp;lt;/tt&amp;gt; class provides the following methods to work with the contents of an array:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (unsigned)'''count'''&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns the number of objects currently in the array.&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (id)'''objectAtIndex:'''(unsigned)index&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns the object located in the array at the index given. Like C- and Java-based arrays, Cocoa array indexes start at 0.&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (BOOL)'''containsObject:'''(id)anObject&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Indicates whether a given object is present in the array.&lt;br /&gt;
&lt;br /&gt;
To practice working with arrays do as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;In Project Builder, create a new Foundation Tool (File → New Project → Tool → Foundation Tool) named &amp;quot;arrays&amp;quot;, and save it in your ''~/LearningCocoa''folder.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Open the ''main.m''file, located in the &amp;quot;Source&amp;quot; group, and modify it to match the following code:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''   	NSString * string = @&amp;quot;one two buckle my shoe&amp;quot;;                     // a'''&lt;br /&gt;
                         '''   	NSArray * array = [string componentsSeparatedByString:@&amp;quot; &amp;quot;];       // b'''&lt;br /&gt;
                         '''	int count = [array count];                                         // c'''&lt;br /&gt;
                         '''   	int i;'''&lt;br /&gt;
                         '''	for ( i = 0; i &amp;lt; count; i++ ) {'''&lt;br /&gt;
                         '''&amp;lt;nowiki&amp;gt;	   printf(&amp;quot;%i: %s\n&amp;quot;, i, [[array objectAtIndex:i] UTF8String]);    // d&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
                         '''	}'''&lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The code we added performs the following tasks:&lt;br /&gt;
&lt;br /&gt;
# Declares a new string.&lt;br /&gt;
# Creates an array of string objects using the &amp;lt;tt&amp;gt;componentsSeparatedByString:&amp;lt;/tt&amp;gt; method of the &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; class. Note that in the first example of this chapter, where we looked for the range of the comma to split the spring, we could have used this method to get the two strings.&lt;br /&gt;
# Obtains the count of the array to use in the &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop.&lt;br /&gt;
# Prints each item of the array to the console.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run ([[Image:Learning Cocoa with Objective-C_I_4_tt115.png|]]-R) the application. You should see output similar to the following appear in the console:&lt;br /&gt;
&lt;br /&gt;
 0: one&lt;br /&gt;
 1: two&lt;br /&gt;
 2: buckle&lt;br /&gt;
 3: my&lt;br /&gt;
 4: shoe&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Using the debugger to explore NSArray ====&lt;br /&gt;
&lt;br /&gt;
We'll explore a few more &amp;lt;tt&amp;gt;NSArray&amp;lt;/tt&amp;gt; methods using the debugger:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Set a breakpoint after the &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop. If you typed in the code exactly as noted previously, including the spaces and the comments that are part of the ''main.m'' file template, the breakpoint will be on line 15.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and debug ([[Image:Learning Cocoa with Objective-C_I_4_tt117.png|]]-Y) the application. Execution will start and then pause at the breakpoint we set.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Click on the Console tab to open up the debugger console.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Type in the following at the &amp;lt;tt&amp;gt;(gdb)&amp;lt;/tt&amp;gt; prompt:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''print-object [array objectAtIndex:4]'''&lt;br /&gt;
                         &lt;br /&gt;
&lt;br /&gt;
You should see the following output:&lt;br /&gt;
&lt;br /&gt;
 shoe&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Type in the following:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''print (int) [array containsObject:@&amp;quot;buckle&amp;quot;];'''&lt;br /&gt;
                         &lt;br /&gt;
&lt;br /&gt;
You should see the following output:&lt;br /&gt;
&lt;br /&gt;
 $1 = 1&lt;br /&gt;
&lt;br /&gt;
This indicates that the array did contain the string we specified. Try using a string that isn't in the array, and see what the return value is. You should see a return value of &amp;lt;tt&amp;gt;0&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Quit the debugger, and close the project.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mutable Arrays ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;NSMutableArray&amp;lt;/tt&amp;gt; class provides the functionality needed to manage a modifiable array of objects. This class extends the &amp;lt;tt&amp;gt;NSArray&amp;lt;/tt&amp;gt; class by adding insertion and deletion operations. These operations include the following methods:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (void)'''addObject:'''(id)anObject&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Inserts the given object to the end of the receiving array.&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (void)'''insertObject:'''(id)anObject '''atIndex:'''(unsigned index)&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Inserts the given object to the receiving array at the index specified. All objects beyond the index are shifted down one slot to make room.&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (void)'''removeObjectAtIndex:'''(unsigned index)&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Removes the object from the receiving array located at the index and shifts all of the objects beyond the index up one slot to fill the gap.&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (void)'''removeObject:'''(id)anObject&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Removes all occurrences of an object in the receiving array. The gaps left by the objects are removed by shifting the remaining objects.&lt;br /&gt;
&lt;br /&gt;
The following steps will explore these methods:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;In Project Builder, create a new Foundation Tool (File → New Project → Tool → Foundation Tool) named &amp;quot;mutablearrays&amp;quot;, and save it in your ''~/LearningCocoa''folder.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Open the ''main.m''file, located in the &amp;quot;Source&amp;quot; group, and modify it to match the code shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-EX-4|Example 4-4]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-4-EX-4&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 4-4. Working with mutable arrays'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''&amp;lt;nowiki&amp;gt;    NSMutableArray * array = [[NSMutableArray alloc] init];             // a&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
 &lt;br /&gt;
                            '''    [array addObject:@&amp;quot;sheryl crow&amp;quot;];                                   // b'''&lt;br /&gt;
 &lt;br /&gt;
                            '''    [array addObject:@&amp;quot;just wants to have fun&amp;quot;];                        // c'''&lt;br /&gt;
 &lt;br /&gt;
                            '''&amp;lt;nowiki&amp;gt;    printf(&amp;quot;%s\n&amp;quot;, [[array description] UTF8String]);                   // d&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
                            '''    [array release];                                                    // e'''&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code we added in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-EX-4|Example 4-4]] performs the following tasks:&lt;br /&gt;
&lt;br /&gt;
# Creates a new mutable array&lt;br /&gt;
# Adds an object to the array&lt;br /&gt;
# Adds another object to the array&lt;br /&gt;
# Prints the array&lt;br /&gt;
# Releases the array, since we created it using the &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; method&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run ([[Image:Learning Cocoa with Objective-C_I_4_tt122.png|]]-R) the application. You should see the following output in the console:&lt;br /&gt;
&lt;br /&gt;
 (&amp;quot;sheryl crow&amp;quot;, &amp;quot;just wants to have fun&amp;quot;)&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Exploring NSMutableArray with the debugger ====&lt;br /&gt;
&lt;br /&gt;
We'll further explore the &amp;lt;tt&amp;gt;NSMutableArray&amp;lt;/tt&amp;gt; class using the debugger:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Set a breakpoint before the line of code that releases the array (line 10).&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and debug ([[Image:Learning Cocoa with Objective-C_I_4_tt124.png|]]-Y) the application. Execution will start and then pause at the breakpoint.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Click on the Console tab to open up the debugger console.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;First, we insert an object into the array after the first object. Then we'll print it out to see the modified array. Type in the following:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''call (void) [array insertObject:@&amp;quot;santa monica&amp;quot; atIndex:1]'''&lt;br /&gt;
 (gdb) '''print-object array'''&lt;br /&gt;
                         &lt;br /&gt;
&lt;br /&gt;
The following output should appear.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;NSCFArray 0x94be0&amp;gt;(&lt;br /&gt;
 sheryl crow,&lt;br /&gt;
 santa monica,&lt;br /&gt;
 just wants to have fun&lt;br /&gt;
 )&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Now remove one of the objects:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''call (void) [array removeObject:@&amp;quot;just wants to have fun&amp;quot;]'''&lt;br /&gt;
 (gdb) '''print-object array'''&lt;br /&gt;
                         &lt;br /&gt;
&lt;br /&gt;
The following will be output:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;NSCFArray 0x94be0&amp;gt;(&lt;br /&gt;
 sheryl crow,&lt;br /&gt;
 santa monica&lt;br /&gt;
 )&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Quit the debugger, and close the project.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Arrays and the Address Book ===&lt;br /&gt;
&lt;br /&gt;
As a quick example of how to use arrays in a situation that isn't so contrived, we will use an API introduced in Mac OS X 10.2—the Address Book API. The Address Book serves as a central contact database that can be used by all applications on the system. The hope is that you won't need a separate contact database for your mailer, for your fax software, etc. Already, the applications that ship with Mac OS X, such as Mail and iChat, utilize the Address Book. The Address Book application is shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-FIG-4|Figure 4-4]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-4-FIG-4&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 4-4. The Address Book'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_4_tt129.png|The Address Book]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use the following steps to guide you in this exploration:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Launch the Address Book application (it is installed in your Dock by default; you can find it in the ''/Applications''folderotherwise), and make sure that you have some contacts defined.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;In Project Builder, create a new Foundation Tool (File → New Project → Tool → Foundation Tool) named &amp;quot;addresses&amp;quot;, and save it to your ''~/LearningCocoa''folder.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Add the Address Book framework to the project by selecting the Project → Add Frameworks menu item. A dialog box will open, asking you to select the framework to add. It should open up to the ''/System/Library/Frameworks''folder. If not, navigate to that folder, and select the ''AddressBook.framework''folderto add to the project. After you click the Add button, a sheet will appear to control how the framework should be added. The settings shown will be fine, and all you need to do is click the Add button again.&lt;br /&gt;
&lt;br /&gt;
This step ensures that Project Builder links against the AddressBook framework, as well as the Foundation framework, when it builds our application.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Open the ''main.m'' file, and modify it to match the following code:&lt;br /&gt;
&lt;br /&gt;
 #import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;br /&gt;
 '''#import &amp;lt;AddressBook/AddressBook.h&amp;gt;                                    // a'''&amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''    ABAddressBook * book = [ABAddressBook sharedAddressBook];          // b'''&lt;br /&gt;
                         '''    NSArray * people = [book people];                                  // c'''&lt;br /&gt;
 &lt;br /&gt;
                         '''    int count = [people count];'''&lt;br /&gt;
                         '''    int i;'''&lt;br /&gt;
                         '''    for (i = 0; i &amp;lt; count; i++) {'''&lt;br /&gt;
                         '''        ABPerson * person = [people objectAtIndex:i];                  // d'''&lt;br /&gt;
                         '''        NSString * firstName = [person valueForProperty:@&amp;quot;First&amp;quot;];     // e'''&lt;br /&gt;
                         '''        NSString * lastName = [person valueForProperty:@&amp;quot;Last&amp;quot;];       // f'''&lt;br /&gt;
                         '''        printf(&amp;quot;%s %s\n&amp;quot;,'''&lt;br /&gt;
                         '''               [lastName UTF8String],'''&lt;br /&gt;
                         '''               [firstName UTF8String]);                                // g'''&lt;br /&gt;
                         '''    }'''&lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The code we added performs the following tasks:&lt;br /&gt;
&lt;br /&gt;
# Imports the AddressBook API set. Without this line, the compiler cannot compile the ''main.m''file, because it won't be able to find the definitions for the Address Book classes.&lt;br /&gt;
# Obtains the Address Book for the logged-in user.&lt;br /&gt;
# Obtains an array containing all of the people in the Address Book.&lt;br /&gt;
# Loops through the people to obtain an &amp;lt;tt&amp;gt;ABPerson&amp;lt;/tt&amp;gt; object. The &amp;lt;tt&amp;gt;ABPerson&amp;lt;/tt&amp;gt; class provides the methods to work with the various attributes that a person record has in the Address Book database.&lt;br /&gt;
# Gets the first name of the person.&lt;br /&gt;
# Gets the last name of the person.&lt;br /&gt;
# Prints the name of the person out to the console.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
For more information about the various classes in the AddressBook framework, see the files in ''/Developer/Documentation/AdditionalTechnologies/AddressBook''.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run ([[Image:Learning Cocoa with Objective-C_I_4_tt131.png|]]-R) the application. You should see a list of your contacts output in the console. Here's a sample from our run of the application, using the contacts pictured in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-FIG-4|Figure 4-4]]:&lt;br /&gt;
&lt;br /&gt;
 Davidson James Duncan&lt;br /&gt;
 Hunter Jason&lt;br /&gt;
 Ronconi Eleo&lt;br /&gt;
 Horwat Justyna&lt;br /&gt;
 Driscoll Jim&lt;br /&gt;
 Davidson Ted&lt;br /&gt;
 Branham Christine&lt;br /&gt;
 Behlendorf Brian&lt;br /&gt;
 O'Reilly Tim&lt;br /&gt;
 Toporek Chuck&lt;br /&gt;
 Czigany Susan&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We haven't gone into great detail on the use of the AddressBook, but just a little knowledge on arrays has already let you work with this important user data. By the time you're done with this book, just think how dangerous you will be! But no matter how dangerous you get, you should remember to use the Address Book API when you create an application that needs to keep track of contacts. Also, you'll be able to build some pretty neat apps using this data. For example, I'm considering building an application that automatically prints Christmas cards to send to all the contacts that I consider to be friends.&lt;br /&gt;
&lt;br /&gt;
=== Sets ===&lt;br /&gt;
&lt;br /&gt;
Sets—implemented by the &amp;lt;tt&amp;gt;NSSet&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;NSMutableSet&amp;lt;/tt&amp;gt; classes—are an unordered collection of objects in which each object can appear only once. A set can be used instead of an array when the order of elements in the collection is not important, but when testing to see if an object is part of the set (usually referred to as &amp;quot;testing for membership&amp;quot;), speed is important. Testing to see if an object is a member of a set is faster than testing against an array.&lt;br /&gt;
&lt;br /&gt;
=== Dictionaries ===&lt;br /&gt;
&lt;br /&gt;
Dictionaries—implemented in the &amp;lt;tt&amp;gt;NSDictionary&amp;lt;/tt&amp;gt; class—store and retrieve objects using ''key-value'' pairs. Each key-value pair in a dictionary is called an ''entry'' . The keys in a dictionary form a set; a key can be used only once in a dictionary. Although the key is usually a string (an &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; object), most objects can be used as keys.&amp;lt;ref&amp;gt;The object used as a key must respond to the &amp;lt;tt&amp;gt;isEqual:&amp;lt;/tt&amp;gt; message and conform to the &amp;lt;tt&amp;gt;NSCopying&amp;lt;/tt&amp;gt; protocol. Since we have not covered protocols yet, the rule of thumb is that any Cocoa object provided in the Foundation framework can be used as a key. Other objects may not work.&amp;lt;/ref&amp;gt; To enable the retrieval of a value at a later time, the key of the key-value pair should be immutable or treated as immutable. If the key changes after being used to put a value in the dictionary, the value might not be retrievable. The &amp;lt;tt&amp;gt;NSDictionary&amp;lt;/tt&amp;gt; class provides the following methods to work with the contents of an array:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (unsigned)'''count'''&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns the number of objects currently in the dictionary&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (id)'''objectForKey:'''(id)aKey&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns the object that is indexed using the given key in the dictionary&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (NSArray *)'''allKeys'''&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Returns an array containing all of the keys in the dictionary&lt;br /&gt;
&lt;br /&gt;
To practice working with dictionaries:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;In Project Builder, create a new Foundation Tool (File → New Project → Tool → Foundation Tool) named &amp;quot;dictionaries&amp;quot;, and save it in your ''~/LearningCocoa''folder.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Open the ''main.m''file, located in the &amp;quot;Source&amp;quot; group, and modify it to match the code shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-EX-5|Example 4-5]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-4-EX-5&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 4-5. Working with dictionaries'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''    NSArray * keys ='''&lt;br /&gt;
                            '''        [@&amp;quot;one two three four five&amp;quot; componentsSeparatedByString:@&amp;quot; &amp;quot;];        // a'''&lt;br /&gt;
                            '''    NSArray * values ='''&lt;br /&gt;
                            '''        [@&amp;quot;alpha bravo charlie delta echo&amp;quot; componentsSeparatedByString:@&amp;quot; &amp;quot;]; // b'''&lt;br /&gt;
 &lt;br /&gt;
                            '''&amp;lt;nowiki&amp;gt;    NSDictionary * dict = [[NSDictionary alloc] initWithObjects:values&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
                            '''                                                        forKeys:keys];        // c'''&lt;br /&gt;
 &lt;br /&gt;
                            '''&amp;lt;nowiki&amp;gt;    printf(&amp;quot;%s\n&amp;quot;, [[dict description] UTF8String]);                          // d&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code we added in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-EX-5|Example 4-5]] performs the following tasks:&lt;br /&gt;
&lt;br /&gt;
# Creates a new array based on a space-delimited string. This set of objects will serve as the keys for the dictionary.&lt;br /&gt;
# Creates a new array that will serve as the values of the dictionary.&lt;br /&gt;
# Creates a new dictionary with our keys and values.&lt;br /&gt;
# Prints the dictionary, so it can be examined.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run ([[Image:Learning Cocoa with Objective-C_I_4_tt133.png|]]-R) the application. You should see output similar to the following appear in the console:&lt;br /&gt;
&lt;br /&gt;
 {five = echo; four = delta; one = alpha; three = charlie; two = bravo; }&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;This is a representation of the structure of the dictionary. Note that the elements are not stored in any particular order. Remember that the keys form a set in which uniqueness, not order, is critical.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We'll explore this example further using the debugger.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Set a breakpoint after the &amp;lt;tt&amp;gt;printf&amp;lt;/tt&amp;gt; statement. If you typed in the code exactly as listed earlier, the breakpoint will be on line 15.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and debug ([[Image:Learning Cocoa with Objective-C_I_4_tt135.png|]]-Y) the application, open the debugger console, and type the following:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''print (int) [dict count]''' &lt;br /&gt;
                      &lt;br /&gt;
&lt;br /&gt;
The following will be output:&lt;br /&gt;
&lt;br /&gt;
 $1 = 5&lt;br /&gt;
&lt;br /&gt;
This tells us that there are five elements in the collection.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Type the following:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''print-object [dict objectForKey:@&amp;quot;three&amp;quot;]'''&lt;br /&gt;
                      &lt;br /&gt;
&lt;br /&gt;
The following will be output:&lt;br /&gt;
&lt;br /&gt;
 charlie &lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Type the following:&lt;br /&gt;
&lt;br /&gt;
 (gdb) '''print-object [dict allKeys]'''&lt;br /&gt;
                      &lt;br /&gt;
&lt;br /&gt;
The following will be output:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;NSCFArray 0x97800&amp;gt;(&lt;br /&gt;
 two,&lt;br /&gt;
 four,&lt;br /&gt;
 three,&lt;br /&gt;
 one,&lt;br /&gt;
 five&lt;br /&gt;
 )&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Quit the debugger, and close the project.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The strengths of the dictionary classes will become apparent when we discuss how they can hold and organize data that can be labeled, such as values extracted from text fields in a user interface. We'll show this in action in [[Learning Cocoa with Objective-C/Single-Window Applications/Models and Data Functionality|Chapter 9]], when we show how you can work with dictionaries to drive tables in user interfaces.&lt;br /&gt;
&lt;br /&gt;
=== Mutable Dictionaries ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;NSMutableDictionary&amp;lt;/tt&amp;gt; class provides the functionality needed to manage a modifiable dictionary. This class extends the &amp;lt;tt&amp;gt;NSDictionary&amp;lt;/tt&amp;gt; class by adding insertion and deletion operations. These operations include the following methods:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (void)'''setObject:'''(id)anObject '''forKey:'''(id)aKey&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Adds an entry to the dictionary, consisting of the given key-value pair. If the key already exists in the dictionary, the previous object associated with that key is removed from the dictionary and replaced with the new object.&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (void)removeObjectForKey:'''(id)aKey'''&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Removes the key and its associated value from the dictionary. &amp;lt;tt/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;sidebar&amp;quot;&amp;gt;&lt;br /&gt;
'''Working with Numbers'''&lt;br /&gt;
&lt;br /&gt;
Collections can hold only objects; they cannot hold C-based primitive types such as &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;float&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;long&amp;lt;/tt&amp;gt;. However, there will be many cases where you will want to store primitive types in your collections. To allow these types to be manipulated as objects, Cocoa provides the &amp;lt;tt&amp;gt;NSNumber&amp;lt;/tt&amp;gt; class. This class can wrap any C numeric type and defines a group of methods to set and access the value. In addition, it defines a &amp;lt;tt&amp;gt;compare:&amp;lt;/tt&amp;gt; method, allowing two numbers to be compared with each other.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Storing Collections as Files ===&lt;br /&gt;
&lt;br /&gt;
One of the nicer things about Cocoa's collection classes is that they support the writing and reading of collection data to and from files called ''property lists'' , or ''plist'' files. This lets you store your data easily and read it later. In fact, Mac OS X uses property lists extensively to store all kinds of data, such as user preferences, application settings, and system-configuration data. In upcoming chapters, we'll be working with user preferences (also known as ''defaults'') and we will see how Mac OS X uses plists in application bundles.&lt;br /&gt;
&lt;br /&gt;
The methods to support this functionality are relatively simple. For the array and dictionary classes, these methods are as follows:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (id)'''initWithContentsOfFile:'''(NSString *)aPath&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Initializes a newly allocated array or dictionary with the contents of the file specified by the path argument&lt;br /&gt;
;&amp;lt;tt&amp;gt;- (BOOL)'''writeToFile:'''(NSString *)path '''atomically:'''(BOOL)flag&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Writes the contents of an array or dictionary to the file specified by the path argument&lt;br /&gt;
&lt;br /&gt;
To practice working with collections and files do as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;In Project Builder, create a new Foundation Tool (File → New Project → Tool → Foundation Tool) named &amp;quot;collectionfiles&amp;quot;, and save it in your ''~/LearningCocoa''folder.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Open the ''main.m''file, and modify it to match [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-EX-6|Example 4-6]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-4-EX-6&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 4-6. Working with property lists'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;#import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, const char * argv[]) {&lt;br /&gt;
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''&amp;lt;nowiki&amp;gt;    NSMutableArray * array = [[NSMutableArray alloc] init];            // a&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
                            '''    [array addObject:@&amp;quot;San Francisco&amp;quot;];                                // b'''&lt;br /&gt;
                            '''    [array addObject:@&amp;quot;Houston&amp;quot;];'''&lt;br /&gt;
                            '''    [array addObject:@&amp;quot;Tulsa&amp;quot;];'''&lt;br /&gt;
                            '''    [array addObject:@&amp;quot;Juneau&amp;quot;];'''&lt;br /&gt;
                            '''    [array addObject:@&amp;quot;Pheonix&amp;quot;];'''&lt;br /&gt;
 &lt;br /&gt;
                            '''    [array writeToFile:@&amp;quot;cities.plist&amp;quot; atomically:YES];                // c'''&lt;br /&gt;
 &lt;br /&gt;
                            '''    NSString * plist =''' &lt;br /&gt;
                            '''        [NSString stringWithContentsOfFile:@&amp;quot;cities.plist&amp;quot;];           // d'''&lt;br /&gt;
                            '''    printf(&amp;quot;%s\n&amp;quot;, [plist UTF8String]);                                // e'''&lt;br /&gt;
 &lt;br /&gt;
                            '''    [array release];                                                   // f'''&lt;br /&gt;
 &lt;br /&gt;
     [pool release];&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code we added in[[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-EX-6|Example 4-6]] does the following things:&lt;br /&gt;
&lt;br /&gt;
# Creates a new mutable array.&lt;br /&gt;
# Adds a series of strings to the mutable array.&lt;br /&gt;
# Writes the array to a file named ''cities.plist''. Since this is not an absolute path, it will be written in the working directory of application. In our case, this file will be written in ''~/LearningCocoa/collectionfiles/build/cities.plist''.&lt;br /&gt;
# Creates a new string based on the contents of the file that we just wrote. Once again, we use a relative path.&lt;br /&gt;
# Prints the contents of the file to the console.&lt;br /&gt;
# Returns the array object that we created.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Build and run ([[Image:Learning Cocoa with Objective-C_I_4_tt142.png|]]-R) the application. You should see output similar to the following appear in the console:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE plist PUBLIC &amp;quot;-//Apple Computer//DTD PLIST 1.0//EN&amp;quot; &amp;quot;http://www.apple.&lt;br /&gt;
com/DTDs/PropertyList-1.0.dtd&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;plist version=&amp;quot;1.0&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;array&amp;gt;&lt;br /&gt;
    &amp;lt;string&amp;gt;San Francisco&amp;lt;/string&amp;gt;&lt;br /&gt;
    &amp;lt;string&amp;gt;Houston&amp;lt;/string&amp;gt;&lt;br /&gt;
    &amp;lt;string&amp;gt;Tulsa&amp;lt;/string&amp;gt;&lt;br /&gt;
    &amp;lt;string&amp;gt;Juneau&amp;lt;/string&amp;gt;&lt;br /&gt;
    &amp;lt;string&amp;gt;Pheonix&amp;lt;/string&amp;gt;&lt;br /&gt;
&amp;lt;/array&amp;gt;&lt;br /&gt;
&amp;lt;/plist&amp;gt;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is an XML representation of the array. This data can be edited with a text editor, transmitted across the Internet, or turned back into a collection of strings in another Cocoa program.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;sidebar&amp;quot;&amp;gt;&lt;br /&gt;
'''Property Lists'''&lt;br /&gt;
&lt;br /&gt;
Mac OS X uses ''property lists'', frequently referred to as ''plists'', to organize data into a form that is meaningfully structured, easily transportable, and storable. Property list files are saved in an XML format for easy editing and transportability.&lt;br /&gt;
&lt;br /&gt;
You can see many examples of ''plist'' files in your ''~/Library/Preferences'' folder. Property lists organize data into named values and lists of values using several types directly represented as the following Cocoa objects: &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NSNumber&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Boolean&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NSDate&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NSData&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NSArray&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;NSDictionary&amp;lt;/tt&amp;gt;. They make it easy for applications to store preference data and, the case of the ''Info.plist'' file in an application bundle, communicate information about an application to the system. To take a look at how applications can use pliststo store configuration information, look at the ''plist'' file for the menu bar clock.&lt;br /&gt;
&lt;br /&gt;
# Open the ''~/Library/Preferences'' folder, and locate the ''com.apple.MenuBarClock.plist'' file.&lt;br /&gt;
# Double-click on thefile to open it with the Property List Editor application (located in the ''/Developer/Applications'' folder).&lt;br /&gt;
&lt;br /&gt;
The Property List Editor application can be used to browse the tree of properties. You can also hit the Dump button to see the XML representation of the property list. Be careful not to save any edits you make, as you can severely confuse an application by making the wrong changes here.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Memory Management ==&lt;br /&gt;
&lt;br /&gt;
Memory management is an important subject in programming. Quite a few of the problems encountered by novice application developers are caused by poor memory management. When an object is created and passed around among various &amp;quot;consumer&amp;quot; objects in an application, which object is responsible for disposing of it and when? If an object is not deallocated when it is no longer needed, memory leaks. If the object is deallocated too soon, problems may occur in other objects that assume its existence, and the application will most likely crash.&lt;br /&gt;
&lt;br /&gt;
The Foundation framework defines a mechanism and a policy that ensures that objects are deallocated only when they are no longer needed. We have hinted at it before, but now it is time to explain things.&lt;br /&gt;
&lt;br /&gt;
The policy is quite simple: you are responsible for disposing of all objects that you own. You own objects that you create, either by allocating or copying them. You also own (or share ownership in) objects that you retain. The flip side of this rule is that you should never release an object that you have not retained or created; doing so will free the object prematurely, resulting in bugs that are hard to track down, even though the fix is simple.&lt;br /&gt;
&lt;br /&gt;
=== Object Initialization and Deallocation ===&lt;br /&gt;
&lt;br /&gt;
As discussed in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/Object-Oriented Programming with Objective-C|Chapter 3]], an object is usually created using the &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; method and is initialized using the &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method (or a variant of the &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method). When an array's &amp;lt;tt&amp;gt;init&amp;lt;/tt&amp;gt; method is invoked, the method initializes the array's instance variables to default values and completes other startup tasks. For example:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;NSArray * array = [[NSArray alloc] init];&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When done with an object that you created, you send the &amp;lt;tt&amp;gt;release&amp;lt;/tt&amp;gt; message to the object. If no other objects have registered an interest in the object, it will be deallocated and removed from memory.&lt;br /&gt;
&lt;br /&gt;
When an object is deallocated, the &amp;lt;tt&amp;gt;dealloc&amp;lt;/tt&amp;gt; method is invoked, giving the object an opportunity to release objects it has created, free allocated memory, and so on. We saw this in action in Chapter 3, when we added the &amp;lt;tt&amp;gt;dealloc&amp;lt;/tt&amp;gt; method to the &amp;lt;tt&amp;gt;Song&amp;lt;/tt&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
=== Reference Counting ===&lt;br /&gt;
&lt;br /&gt;
To allow multiple objects to register interest in another object and yet have this object removed from memory when no other objects are interested in it, each object in Cocoa has an associated reference count. When you allocate or copy an object, its reference count is automatically set to 1. This indicates that the object is in use in one place. When you pass the object to other objects, wanting to make sure the object stays around for their use, they can use the &amp;lt;tt&amp;gt;retain&amp;lt;/tt&amp;gt; method to increment the reference counter.&lt;br /&gt;
&lt;br /&gt;
To visualize this, imagine that we have an object being held in three different arrays, as shown in [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-FIG-5|Figure 4-5]]. Each array retains the object to make sure that it remains available for its use. Therefore, the object has a reference count of 3.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-4-FIG-5&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 4-5. Reference counting'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_4_tt145.png|Reference counting]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever you are done with an object, you send a release message to decrement the reference count. When the reference count reaches 0, the release method will invoke the object's &amp;lt;tt&amp;gt;dealloc&amp;lt;/tt&amp;gt; method that destroys the object. [[Learning Cocoa with Objective-C/Cocoa Overview and Foundation/The Cocoa Foundation Kit#learncocoa2-CHP-4-FIG-6|Figure 4-6]] shows an object being removed progressively from a set of arrays. When it is no longer needed, its retain count is set to 0, and the object is deallocated.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-4-FIG-6&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 4-6. Releasing of an object'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_4_tt146.png|Releasing of an object]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Autorelease Pools ===&lt;br /&gt;
&lt;br /&gt;
According to the policy of disposing of all objects you create, if the owner of an object must release the object within its programmatic scope, how can the owner give that object to other objects? Or, said another way, how do you release an object you would like to return to the caller of a method? Once you return from a method, there's no way to go back and release the object.&lt;br /&gt;
&lt;br /&gt;
The answer is provided by the &amp;lt;tt&amp;gt;autorelease&amp;lt;/tt&amp;gt; method built into the &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; class, in conjunction with the functionality of the &amp;lt;tt&amp;gt;NSAutoReleasePool&amp;lt;/tt&amp;gt; class. The &amp;lt;tt&amp;gt;autorelease&amp;lt;/tt&amp;gt; method marks the receiver for later release by an &amp;lt;tt&amp;gt;NSAutoreleasePool&amp;lt;/tt&amp;gt;. This enables an object to live beyond the scope of the owning object so that other objects can use it. This mechanism explains why you have seen dozens of code examples that contain the following lines:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];&lt;br /&gt;
...code...&lt;br /&gt;
[pool release]; &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each application puts in place at least one autorelease pool (for each thread of control that is running in the application) and can have many more. You put an object in the pool by sending the object an &amp;lt;tt&amp;gt;autorelease&amp;lt;/tt&amp;gt; message. In the case of an application's event cycle, when code finishes executing and control returns to the application object, the application object sends a &amp;lt;tt&amp;gt;release&amp;lt;/tt&amp;gt; message to the autorelease pool, and the pool sends a release message to each object it contains. Any object that reaches a reference count of 0 automatically deallocates itself.&lt;br /&gt;
&lt;br /&gt;
When an object is used solely within the scope of a method that creates it, you can deallocate it immediately by sending it a &amp;lt;tt&amp;gt;release&amp;lt;/tt&amp;gt; message. Otherwise, use the &amp;lt;tt&amp;gt;autorelease&amp;lt;/tt&amp;gt; message for all objects you create and hand off to other objects so that they can choose whether to retain them.&lt;br /&gt;
&lt;br /&gt;
You shouldn't release objects that you receive from other objects, unless you have first retained them for some reason. Doing so will cause their reference count to reach 0 prematurely, and the system will destroy the object, thinking that no other object depends on it. When objects that do depend on the destroyed object try to access it, the application will most likely crash. These kinds of bugs can be hard to track down, even though their cause and fix are simple.&lt;br /&gt;
&lt;br /&gt;
You can assume that a received object remains valid within the method in which it was received and will remain valid for the event loop that is handling it. If you want to keep it as an instance variable, you should send it a &amp;lt;tt&amp;gt;retain&amp;lt;/tt&amp;gt; message and then autorelease it when you are done using it.&lt;br /&gt;
&lt;br /&gt;
=== Retaining Objects in Accessor Methods ===&lt;br /&gt;
&lt;br /&gt;
One of the primary places where you will need to be aware of memory management is in the accessor methods of your classes. At first glance, it is obvious that you will want to release an old object reference and retain the new one. However, because code that calls a class's setter method might call it multiple times with the same object as an argument, the order in which you release and retain the object references is important.&lt;br /&gt;
&lt;br /&gt;
As a rule, you want to retain the new object before releasing the old one. This ensures that everything works as anticipated, even if the new and old objects are the same. If you reverse these steps, and if the new and old objects are actually the same, the object might be removed permanently from memory before being retained.&lt;br /&gt;
&lt;br /&gt;
Here is the ''retain, then release'' rule expressed in code:&lt;br /&gt;
&lt;br /&gt;
 - (void)setProperty:(id)newProperty&lt;br /&gt;
 {&lt;br /&gt;
     [newProperty retain];&lt;br /&gt;
     [property release];&lt;br /&gt;
     property = newProperty;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
There are other ways to ensure connections in setter methods, many of which are valid and appropriate for certain situations. However, this is the simplest possible pattern we can give that will always work. We will use this pattern throughout the book.&lt;br /&gt;
&lt;br /&gt;
=== Rules of Thumb ===&lt;br /&gt;
&lt;br /&gt;
The important things to remember about memory management in Cocoa distill down to these rules of thumb:&lt;br /&gt;
&lt;br /&gt;
# Objects created by &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;copy&amp;lt;/tt&amp;gt; have a retain count of 1.&lt;br /&gt;
# Assume that objects obtained by any other method have a retain count of 1 and reside in the autorelease pool. If you want to keep it beyond the current scope of execution, then you must retain it.&lt;br /&gt;
# When you add an object to a collection, it is retained. When you remove an object from a collection, it is released. Releasing a collection object (such as an &amp;lt;tt&amp;gt;NSArray&amp;lt;/tt&amp;gt;) releases all objects stored in it as well.&lt;br /&gt;
# Make sure that there are as many &amp;lt;tt&amp;gt;release&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;autorelease&amp;lt;/tt&amp;gt; messages sent to objects as there are &amp;lt;tt&amp;gt;alloc&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;copy&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;mutableCopy&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;retain&amp;lt;/tt&amp;gt; messages sent. In other words, make sure that the code you write is balanced.&lt;br /&gt;
# Retain, then release objects in setter methods.&lt;br /&gt;
# &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; objects created using the &amp;lt;tt&amp;gt;@&amp;quot; . . . &amp;quot;&amp;lt;/tt&amp;gt; construct are effectively constants in the program. Sending retain or release messages to them has no effect. This explains why we haven't been releasing the strings created with the &amp;lt;tt&amp;gt;@&amp;quot; . . . &amp;quot;&amp;lt;/tt&amp;gt; construct.&lt;br /&gt;
&lt;br /&gt;
If you apply these rules of thumb consistently and keep the retain counts of your objects balanced, you can manage memory in your applications effectively.&lt;br /&gt;
&lt;br /&gt;
== Exercises ==&lt;br /&gt;
&lt;br /&gt;
# Investigate the &amp;lt;tt&amp;gt;lowercase&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;uppercase&amp;lt;/tt&amp;gt; methods of &amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt; using the debugger.&lt;br /&gt;
# Write a Foundation Tool command-line application that prints the contents of any filename given to it.&lt;br /&gt;
# Read the documentation on your hard drive about the &amp;lt;tt&amp;gt;NSArray&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;NSSet&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;NSDictionary&amp;lt;/tt&amp;gt; classes.&lt;br /&gt;
# Modify the arrays example application so that it saves the contents of the array to a file.&lt;br /&gt;
# Write an example that saves a dictionary to disk. Don't just use string objects in the array, but use some other objects like dictionaries and numbers so that you can see how Cocoa saves different types out to XML property lists.&lt;br /&gt;
# Examine the code we've written so far with an eye for how memory is managed. (A bug regarding memory management has been left in one of the examples.)&lt;br /&gt;
== Notes ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Docbook2Wiki</name></author>	</entry>

	</feed>