Chapter 3. Digging Deeper Into Objective-C

In This Chapter

  • Working through the standard way to do initialization

  • Initializing superclasses and subclasses

  • Managing memory

Chapter 1 of this minibook explains that the birth of an object starts with alloc and init. It is alloc that sets aside some memory for the object and returns back a pointer to that memory. This fact is important to keep in mind, because after you create these new objects, you become responsible for managing them, as I show in this chapter. Managing the memory allocated for your objects can be one of the few real hassles in programming with Objective-C.

Warning

Memory management is not glamorous, but it trumps cool in an application. In fact, memory management is probably the single most vexing thing about iPhone programming. It has made countless programmers crazy, and I can't stress enough how important it is to build memory management into your code from the start. Retrofitting can be a nightmare. (Co-author Neal still has dreams in which hell is having to go back through an infinite number of lines of code and retrofit memory management code.)

The init method is named for initialization, which I introduce in Chapter 1 of this minibook. This chapter spells out some of your initialization options.

Initializing Objects

Initialization is the procedure that sets the instance variables of an object to a known initial state. Essentially, you need to initialize an object to assign initial values to these variables. Although ...

Get iPhone® Application Development All-In-One For Dummies® 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.