Introducing Objective-C (iOS 4)

In this topic, we’ll examine all of the Objective-C elements that are applicable to iOS development. We assume that you have a good understanding of a rigorous programming language (like C), that you know the basic concepts behind object-oriented programming (OOP), and that you understand what the Model-View-Controller (MVC) architectural model is.

We’re now ready to move into the world of SDK development. We’ll take a quick tour to examine the programming language and frameworks you’ll be using when you program with the SDK.

All of the SDK’s programming is done in Objective-C, a full superset of C, allowing you to write any traditional C code. (There is also Objective-C++, which allows for full integration of Objective-C and C++, with some caveats.) It adds powerful object-oriented capabilities as well. These extensions come by way of the design philosophies of Smalltalk, one of the earliest object-oriented languages. Because of its origin beyond the standard boundaries of C, Objective-C’s messaging code may look a little strange to you at first. But after you get the hang of it, you’ll discover that it’s elegant and easy to read, providing some nice improvements over traditional ANSI C code.

We’ll look at Objective-C’s messages, class definitions, properties, compiler directives, categories, and protocols. Although this overview gives you enough to get started with Objective-C, it can’t provide all the details, particularly for more complex functionality like properties and categories. If you need more information than we’ve been able to provide, look at Apple’s own references on the topic, particularly "Learning Objective-C: A Primer," "Object-Oriented Programming with Objective-C," and "The Objective-C 2.0 Programming Language," all of which can be found in Apple’s iOS developer library.


Let’s start with a look at Objective-C’s big picture. It’s an object-oriented language, which means it’s full of classes and objects, instance variables, and methods.

As implemented by Apple and used throughout iOS’s frameworks, Objective-C is built entirely around objects. Windows, views, buttons, sliders, and controllers all exchange information with each other, respond to events, and pass actions in order to make your program run.

A header (.h) file and a source code (.m) file together represent each object in Objective-C. Sometimes you’ll access standard classes of objects that come built into the iOS frameworks, but often you’ll instead subclass objects so that you can create new behaviors. When you do this, you’ll add a new header file and source code file to your project that together represent the new subclass you’ve invented.

Although we won’t dwell on it much, note that C++ code can be mixed in with Objective-C code. We leave the specifics of that for the experienced object-oriented programmer (and, as usual, there’s more detail on Apple’s website). You can also freely insert older C syntax; as we’ll discuss shortly; this is necessary when you’re working with older libraries.

With all that said, we’re ready to dive into Objective-C’s unique syntax. Table 2.1 summarizes the seven major elements of syntax.

Table 2.1 Objective-C code can look different from ANSI C; it depends on a handful of syntactic changes.

Syntax element

Summary

Messages

Messages send commands to objects in [bracketed] code (similar to functions in C).

Classes

Classes define object types in matched .h and .m files.

Properties

Properties allow for the easy definition of accessors and mutators (setting and getting object member variables).

Categories

Categories can be used to add to classes without subclassing.

Table 2.1 Objective-C code can look different from ANSI C; it depends on a handful of syntactic changes.

Syntax element

Summary

Protocols

Protocols define methods that a class promises to respond to (similar to interfaces in languages like Java).

tmp12-16

@ directives are used by the compiler for a variety of purposes.

tmp12-17

With iOS 4, blocks were introduced. Blocks are objects that encapsulate a unit of work—or, in less abstract terms, a segment of code—that can be executed at any time. The caret symbol (*) is used as a syntactic marker for blocks.

We offer a more technical summary at the end of this section, showing all the syntax of these elements. But first, we discuss these syntactic elements at length, in approximate order of importance.

Next post:

Previous post: