Chapter 5. Creating classes

 

This chapter covers

  • Creating custom Objective-C classes
  • Adding instance variables
  • Adding class and instance methods
  • Adding properties
  • Implementing init and dealloc

The real power of object-oriented programming comes into play when you start to create your own custom objects that perform application-specific tasks, and this is what we explore in this chapter. By creating a custom class, you can encapsulate a common set of functionality into a reusable component that you can use repeatedly throughout an application (or even a set of applications).

To put the concepts into context, we concentrate on updating the Rental Manager application to use object-oriented design principles, replacing the Rental-Property structure with a class that provides equivalent functionality. The new class will store and maintain the details of a single rental property, which means you must specify what data each object should keep track of and add instance and class methods to allow safe and controlled access to that data.

We also cover the concept of properties and how they make calling common methods that set or get the value of a field stored by an object easier and quicker. Let’s start by defining the shell of the new class.

5.1. Building custom classes

Creating a new class in Objective-C is relatively straightforward and typically consists of three steps:

5.2. Declaring the interface of a class

5.3. Providing an implementation for a class

5.4. Declared properties

5.5. Creating and destroying objects

5.6. Using the class in the Rental Manager application

5.7. Summary

sitemap