Programming Mac OS X with Cocoa for Beginners/Getting around in Xcode and Interface Builder
We will shortly write our first Mac application in Cocoa, good old "Hello World!". While not very useful, this will give you confidence that your developer installation is working as it should.
XCode 3 and EarlierEdit
When Xcode first starts, it may not create any windows but it will have a menu bar and you can create a project and open its window by choosing: File→New Project...
An assistant opens with all the types of projects that Xcode can build. We want to make a simple Cocoa application, so scroll down the list to find "Cocoa Application", select it and click Next.
In the upper field, type the name of the project. We will call it "HelloWorld". In the lower field, Xcode wants the path to the Projects folder. You could type it, but it's safer to use the "Choose..." button to navigate to it. Find wherever you put it, and click Choose. The path will be filled in. Click Finish.
Xcode opens a window with the title of your project — in this case "HelloWorld". Down the left hand side of the window is a hierarchical list which lists all of the various elements that make up the program. The first item is selected, called "HelloWorld". The main part of the window lists a number of files, such as 'Cocoa.framework', but they are not important right now. The top part of the window is the toolbar with a number of buttons, such as "Build", "Info", etc.
Let's build and run the project, to see what it does. Click the "Build and Run" ("Build and Go" on older versions of Xcode) button. Xcode will churn away for a minute, compiling various files. Shortly it will finish, the toolbar will change to the running state, and the application will be launched. A new window with the title "Window" will open with nothing in it, and the menu bar will show the name of the application, HelloWorld.
You will see that while this isn't a very useful application, it does exhibit much of the basic behaviour that any Mac application would be expected to. You can drag the window around, resize it, choose items from the menu, and so forth. It even has an "about box"! Not bad for not even writing any code yet. When you're done, choose "Quit" and control will return to Xcode. If you want to run it again, you can click the "Run" button.
On returning to Xcode, you are in the "build" view. To return to the original project view, click the leftmost button at the top left of the toolbar, below the window close button.
We'll now jump into Interface Builder (IB) to make our application do something. While you could launch Interface Builder from the Finder, Xcode works hand in hand with it, so it's usually easier to let Xcode handle this. To start IB then, find the item "NIB Files" at the bottom of the list on the left. Open it, and double-click 'MainMenu.nib'. IB will launch and open that file in one go.
MainMenu.nib contains the resources for the basic application. This includes the default menu bar and the simple window we already saw. The main view of a file in IB is a graphical list of the objects it contains, just like files in the Finder. Thus there is the MainMenu object, the Window object, and a couple of others, such as 'File's Owner' and 'First Responder' which will be explained in detail much later. Double-click on the icon for Window. The actual window will open. The size and position of this window is exactly what will show up in the finished app, so put it where you want it.
Let's add some text to the window. You should see a palette called Cocoa-Controls. Click the Text button which is third from the left. The window title will now be Cocoa-Text, and you will see various examples of text. Find the 'System Font Text' item in the pallete. (Note: In newer versions of Xcode and Interface Builder, the Cocoa-Control palette has been replaced with a content library, and 'System Font Text' items have been replaced with 'labels'. In Xcode 3.2.6, for example, in Tools→Library→Objects, indented under 'Library' and 'Views & Cells' select 'Inputs & Values', then below choose 'Label'.) Drag the 'System Font Text' item from the palette (or 'Label' item from the library) into the window and drop it there. It will appear with a set of selection 'handles' around it. Double-click on the text to select it, and change the text to "Hello World!". Click outside of the text to deselect it.
Choose File→Save and return to Xcode. Click "Build and Run". When your application launches, it now includes the text you just added.
Well, so far not so very impressive, but it means that at this stage, everything is working as it should. We can now proceed to more interesting and challenging things with confidence in our tools.
Xcode comes with the complete Apple Developer Reference Library locally installed. It is fully integrated into the Xcode editor — to look up any method or class within Cocoa, simply option-double-click the class or method name anywhere in any text editor, or you can command-click on the identifier in the source-code and be taken to the declaration of that identifier in the .h file. Remember, all Cocoa classes start with 'NS...' (this stands for NextStep, the predecessor to Mac OS X where the Cocoa API first appeared as the programming environment itself. Because of this, avoid the temptation to call your own classes 'NS...' anything.
There are other APIs available to use to develop software that runs on Mac OS X include Carbon, Toolbox for the Classic environment, POSIX, and Java), making them easy to spot. Though, Carbon and Classic aren't really supported by Apple anymore. They were for providing developers with an upgrade path from old version of Classic Mac OS (before Cocoa). You can still use Java, but Apple has lessened its support of Java. Java is good for cross-platform work where you want to develop apps that will run on Mac or Windows. The problem with Java is it reduces the application software to the "least common denominator" disallowing the use of many APIs in Cocoa and Mac OS X. The Mac App Store will not accept applications written in Java.
Although there are a number of APIs available to program the Mac, Cocoa and Objective-C are "native" allowing you to develop apps that are truly "Mac-like".
You can open the documentation window at any time using Help→Documentation. This will take you to the documentation that was installed when you installed XCode, etc. on your computer.
The documentation library can also be updated by going to the Xcode Preferences (Xcode→Preferences), and choosing the Documentation option.
As well as complete descriptions of all Cocoa classes and methods, it also has a number of extended discussions about particular aspects of Cocoa programming. These are well worth reading once you have picked up the basics, perhaps from here.