Programming Mac OS X with Cocoa for Beginners 2nd Edition/First Cocoa Application - Hello World
We will shortly write our first Mac application in Cocoa called "Hello World!". Whether you're a beginner to programming or an experience professional, it's good to write a first program using new tools to make sure that the tools are setup correctly and that you have a basic understand of how to use the tools.
Creating a New Cocoa ProjectEdit
- Open Xcode
- From the "File" menu, choose "New"
- A submenu will appear with choices of what you can create a new one of
- Choose to create a "New Project..."
- You have the option of creating an app for iOS devices or creating an app for Mac OS X. Under "OS X", select "Application". Then select "Cocoa Application". Then click the "Next" button.
Setting the Project OptionsEdit
We're only creating a simple HelloWorld project, but since we have to go through the Project Settings Window, let's review what each of these project settings does. Although these settings can be changed after a project is created, it's just easier to set them here and get it right the first time.
- Project Name In this project, we will use the project name: "HelloWorld". Note that there are no spaces in "HelloWorld". Generally, it is better not to have spaces to make things easier to deal with.
- Company Identifier You can leave this blank for this HelloWorld project, but if you ever develop an application to sell or give away on Apple's Mac App Store, you'll need a unique company identifier such as a reversed domain name. For example, for the fictional company: example.com they would start their company identifier with "com.example." This assures that no other company will use the same bundle identifier for their apps. It would be good to add "mac" to this so that iOS apps wouldn't have the same bundle identifier either. So, in this example a good company identifier might be "com.example.mac".
- Bundle Identifier The bundle identifier is calculated automatically by Xcode. It is the company identifier with the project name appended to the end. This is called the "Bundle Identifier". A Mac app is one kind of bundle, but there are other kinds of bundles. In this book, we'll only be covering application bundles, or "apps" for short.
- Class Prefix The class prefix is used so that classes written by different developers don't use the same identifiers to name their classes which could make class libraries incompatible because they could use the same class names. To avoid this problem with Cocoa, Next Step developers (who created the code that eventually became Apple's Mac OS X) used the class prefix "NS" which means "Next Step". You'll see this all of the time in Cocoa. NSWindow, NSDocument, NSApplication, etc. Apple uses other class prefixes, such as "CF" for "Core Foundation", and "CG" for "CoreGraphics". It is recommended that you use a class prefix such as your initials or something so that your class names are more likely to be unique. You might as well enter a class prefix now. You can always change it later for new apps that you develop. In this wikibook, we will use a class prefix of "WB" which stands for "WikiBooks". So set your class prefix to "WB" while working with this wikibook. For your own project you're encouraged to use your own class prefix.
- App Store Category The app store category is filled out if you're going to sell your Mac app at Apples Mac app store. For the projects in this wikibook, you can leave it set to "None". For your own projects, you can select a category if you plan to have Apple sell your app on it's Mac App Store. (In this wikibook, we always write "Mac App Store" to distinguish this store from the iOS App Store.
- Create Document Based Application When you check this checkbox, Xcode uses a different template that sets up your application, by default, to handle much of what a document-based application needs to do. For example, if you were writing a text editor, you would check this checkbox so that it would be easy to program the opening and saving of documents. For this HelloWorld project, you don't need to check this checkbox.
- Document Extension This is the filetype that your document-based application can open. We didn't check "Create Document Based Application" so this setting won't be used.
- Use Core Data Core data is an option that you have to help you save your documents to disk. You can leave it unchecked for HelloWorld since we don't have any documents to save to disk.
- Include Unit Tests unit testing is a way to write code so that your application can test itself. This is a great idea, but in our simple HelloWorld application, we don't need any unit-testing so you can leave this checkbox unchecked.
- Include Spotlight Importer w:Spotlight_(software)Spotlight is a feature of Mac OS X that allows the user to quickly search their disk. This app doesn't create any documents so we can leave this checkbox unchecked.
Once you have set all of your project options set, click the "Next" button to continue.
Saving the ProjectEdit
Next, a window will be displayed allowing you to select where to save your new project.
Look down near the bottom of the window. There is a checkbox that allows you to use Git. This HelloWorld project is simple enough that we don't need source control , but source control is a great feature that allows you to go "back in time" if your code gets messed up. Source control can also be used when you have more than one programmer working on a project and they need to coordinate changes to the source code. Xcode supports Git to do source control with the ease of clicking a button. (If you're used to using different software besides Git for source code control, you still can, it's just that Git is built-in to Xcode and is pretty good.)
Building User-Interfaces With Xcode 4Edit
You may read about an application called "Interface Builder" (IB) to build your Mac app's user interface such things as menus and windows and such. With the release of Xcode 4, there is no longer a separate application called "Interface Builder" that is installed along with Xcode. Now, the functionality of Interface Builder is built into Xcode 4.
In Xcode, in the HelloWorld project window. In the upper, left-hand corner, there will be a list of files in the project. One of these files will be called "HelloWorld.xib". This is where the user interface definitions are held. In older versions of Apple's developer tools, these user interface definitions were held in a file with an extension of .nib and were called "nib files". Now, they're held in a file with an extension of ".xib" and are called "zib files" (though sometimes developers still call them "nib files") It's the same thing, just a newer format. nib files how the definitions of your GUI objects such as menu and windows.
Click on the file "HelloWorld.xib" and Xcode 4 will display the user interface definitions in the middle window pane of its window. You should be able to see a menu bar which has all of the menus that are standard to a Mac OS X application, such as the Apple Menu, the File Menu, the Edit Menu, etc. In the interface editor, you can click on one of these menu titles and the associated menu will be shown. For example, if you click on "File" in the menu bar, you will be shown the "File" menu, you'll see the menu items: "New", "Open", etc. Not only did Xcode create a user interface for you, much of the user interface actually will work without writing any code.
Besides the menu bar, there will be a window available for you to edit in Xcode 4. What we are going to do is to add the text "Hello World" to the window.
- On the left-hand side of the Xcode 4 project window, click on HelloWorld.xib. If you can't see the file "HelloWorld.xib", you may need to go to the "View" menu and choose "Navigators" and then choose "Show Project Navigator". There are so many things that can be viewed on the screen that sometimes you have to hide one thing to be able to see other things. Now would be a good time to experiment with the View menus to learn how to get around Xcode 4.
- Now, you should be able to see a window with a title of "HelloWorld".
- Now, from the lower, right-hand corner, there should be a list of user-interface items to choose from. It will probably have "Push Button", etc. If you can't see this, from the "View" menu choose "Utilities" then choose "Show Object Library". This will reveal a list of user-interface items to choose from. Now might be a good time for you to scroll through and look at some of the objects that you can add to your windows with Xcode 4.
- The kind of object that we want to add to our window in this project is a "Label". A label is used when you just want to easily put a small amount of text into a window. Now, click on the Label icon and drag and drop it onto your window.
- Now you can change the text that is in the label from the default to be "Hello World".
Running the ProgramEdit
Between Xcode starting us out with a pattern or template that is, and Cocoa we have a Mac application that is pretty Mac-like with many features already done for us.
To run your app, from the "Product" menu, choose "Run" and the app should compile and run with a Mac app starting with a standard Mac menu bar and a window with the title "HelloWorld" that has the text "Hello World" in it.
If you have any problems, you can use the following resources to help you:
Xcode comes with the complete Apple Developer Reference Library locally installed that matches the version of the software development kit (SDK) that you are using. 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. In addition to class names that begin with "NS" there are function names, struct names, and constant names that begin with "NS", so just because something begins with "NS" doesn't mean that it's a class name, but it's most likely part of Cocoa.
Although there are other technologies available to program the Mac, Cocoa and Objective-C are "native" allowing you to develop apps that are truly "Mac-like" and can interface more directly with Cocoa, the other frameworks, and with the OS.
You can open the documentation window at any time using the "Help" menu from within Xcode. This will take you to the documentation that was installed when you installed Xcode, etc. on your computer. Sometimes it's also useful to search online.
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.