As with most programming languages, Objective-C is a relatively simplesyntax backed by an extensive standard library. This tutorial focuses mostly onthe language itself, but it helps to have at least some idea of the tools thatyoull be interacting with in the real world.
There are a few different standard libraries out there, butApples Cocoa and CocoaTouch frameworks are by far the most popular. These define the API forbuilding OS X and iOS apps, respectively. The table below highlights someof the key frameworks in Cocoa and Cocoa Touch. For a more detailed discussion,please visit the MacTechnology Overview or iOSTechnology Overview.
After youre comfortable with Objective-C, these are some of the toolsthat youll be leveraging to build iOS and OS X applications. Butagain, this tutorial is not meant to be a comprehensive guide to appdevelopmentits designed to prepare you to use the aboveframeworks. With the exception of the Foundation Framework, we wontactually be working with any of these libraries.
If youre interested in Mac App development, you should take a look atRys Cocoa Tutorial afteryou have a solid grasp on Objective-C. It shows you how to build OS X appsusing the same hands-on methodology as this tutorial.
Xcode
Xcode is Applesintegrated development environment (IDE) for Mac, iPhone, and iPad appdevelopment. It includes not only a source code editor, but also an interfacebuilder, a device simulator, a comprehensive testing and debugging suite, theframeworks discussed in the previous section, and everything else you need tomake apps.
While there are other ways to compile Objective-C code, Xcode is definitelythe easiest. We strongly recommended that you install Xcode now so you canfollow along with the examples in this tutorial. It is freely available throughthe MacApp Store.
Creating an Application
Xcode provides several templates for various types of iOS and OS Xapplications. All of them can be found by navigating to File > New >Project... or using the Cmd+Shift+N shortcut. This will open adialog window asking you to select a template:
Creating a command line application
For this tutorial, well be using the Command Line Tooltemplate found under OS X > Application, highlighted in theabove screenshot. This lets us strip away all of the elements specific toiOS/OS X and focus on Objective-C as a language. Go ahead and create a newCommand Line Tool now. This opens another dialog asking you toconfigure the project:
Configuring a command line application
You can use whatever you like for the Product Name andOrganization Name fields. For the Company Identifier useedu.self
, which is the canonical private use identifier. Forproduction applications, youll need to get a real company ID from Appleby registering as adeveloper.
This tutorial utilizes several classes defined in the Foundation Framework,so be sure to select Foundation for the Type field. Finally,the Use Automatic Reference Counting checkbox should always beselected for new projects.
Clicking Next prompts you for a file path to store the project(save it anywhere you like), and you should now have a brand new Xcode projectto play with. In the left-hand column of the Xcode IDE, youll find afile called main.m
(along with some other files and folders). Atthe moment, this file contains the entirety of your application. Note that the.m
extension is used for Objective-C source files.
main.m
in the Project Navigator
To compile the project, click the Run button in the upper-leftcorner of the IDE or use the Cmd+R shortcut. This should displayHello, World!
in the Output Panel located at the bottomof the IDE:
Xcodes Output Panel
The main() Function
As with plain old C programs, the main()