Learn Objective-C on the Mac - Mark Dalrymple [13]
Figure 2-21. Xcode asks you how you want to import the object.
Make sure the box is selected, as in Figure 2-21, and then press the Add button. That will add this file to your Xcode project and also copy it into the project folder. Now, whenever you build your project, Xcode will include this file when it builds your application. That’s not enough to make the file your application’s icon, however. An application could potentially contain more than one icon file, so you have to explicitly tell Xcode which file to use for the application’s main icon.
To do that, single-click on the file Info.plist under the Resources group in the Groups & Files pane, which will open that file up in the editing pane (Figure 2-22). Info.plist contains a variety of information about your application. One of the items of information it holds is the name of the application’s icon file.
Figure 2-22. Info.plist opened in the editing pane so you can specify the name of this application’s icon file
Property Lists
Info.plist is a special kind of file known as a property list. Property lists are used extensively throughout OS X. Although end users rarely see them, they are used in many parts of Cocoa development, so you will see them a lot.
Property list files are made up of a list of entries. Each entry is made up of a key and a value. Figure 2-22 shows Xcode’s built-in property list editor, editing the file Info.plist. Each row represents a single entry. As you can see, the property list consists of two columns. The left column is labeled Key and the right column is labeled Value.
NOTE: Property lists also have the ability to store multiple values under a single key. It is possible to store either an array (or list) of items under a single key, or even to store another whole set of keys and values under one key. It’ll be a while before we need that functionality, but we figured you should know it was possible.
In Figure 2-22, the entry with the key Icon file is highlighted. The value associated with that key, shown in the right column, should be blank. Double-click the empty column to the right of the Icon file key, and type hello world.icns. Now press ⌘S to save the property list.
Are you ready to write some code? Well, guess what? There’s none to write. We’re done with our application.
Running Your Application
Build and run your application by selecting Build & Run from the Build Menu or by typing ⌘R. Xcode will build your application, which may take a bit, then run your application. A window should appear containing your centered, colored label. If you look in the Dock at the bottom of your Mac’s main screen, you should see your application represented by the icon you imported into your project. But wait: there’s more!
Select About Hello World from the Hello World menu (Figure 2-23). Not only do you get an About box for free, but it includes your icon.
Figure 2-23. Your application uses your icon in a variety of places, including the about box.
We’re still not done. Move your mouse over the words “Hello World” in your application’s main window. Your cursor should change from an arrow to a text-cursor. Because you made the label selectable, Cocoa automatically changes the cursor as a hint to the user that they can select this text. Go ahead and double-click the word Hello, and it will highlight. Now if you select the Edit menu, you’ll see that the Copy menu item is not grayed out. If you choose it, your program will copy the word “Hello” to the pasteboard and you can then paste it into any other application that accepts text. With Hello still selected, select the Edit menu, then select the Speech submenu, and select Start Speaking. Your application will say “Hello” using your Mac’s text-to-speech capabilities.
Without writing a single line of code, your application supports copying text to the pasteboard, and text-to-speech. With almost no work, your application behaves like