Appendix D. Crash Course in Objective-C

Objective-C is an object-oriented programming language used by Apple primarily for programming Mac OS X and iPhone applications. It is an extension to the standard ANSI C language and hence it should be an easy language to pick up if you are already familiar with the C programming language. This appendix assumes that you already have some background in C programming and focuses on the object-oriented aspects of the language. If you are coming from a Java or .NET background, many of the concepts should be familiar to you; you just have to understand the syntax of Objective-C and in particular pay attention to the section on memory management.

Objective-C source code files are contained in two types of files:

  • .h — header files

  • .m — implementation files

For the discussions that follow, assume that you have created a View-based Application project using Xcode and added an empty class named SomeClass to your project.


If you observe the content of the SomeClass.h file, you will notice that at the top of the file is an #import statement:

#import <Foundation/Foundation.h>

@interface SomeClass : NSObject {



The #import statement is known as a preprocessor directive. In C and C++, you use the #include preprocessor directive to include a file's content with the current source. In Objective-C, you use the #import statement to do the same, except that the compiler ensures that the file is included at most only once. To import a header file from ...

Get Beginning iPhone® SDK Programming with Objective-C® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.