This tutorial will teach you fundamentals object oriented programming concepts from the perspective of the Objective-C programming language.

Object Oriented Programming (OOP) is a programming methodology used for creating objects that interact with each other in an iOS application. Object-Oriented Programming in really boils down to four fundamental units: classe, object, message sending, inheritance. Objective-C language support all of them.

Class and Object

A class is a blueprint or template for how to create a specific object. A class blueprint defines the object’s properties and methods. Properties define the object’s data. Methods defines what you can be done to the object’s data. This table shows what a shopping list class and objects instantiated (created) from it looks like. The table also list the class’ Properties and Methods.

The List Class have these properties.

  • list name
  • item number
  • item name
  • item status (completed/pending)

The List Class have these methods.

  • add item
  • view all items
  • update item
  • delete item
  • find item
listclass_object

You write object-C code to create a class in a class file. You then instantiate one or more objects from the class when needed. Objective-C class code goes in  two files: an interface (header) file and an implementation file, as shown below.

codepractice_viewcontroller_class

You write code in the interface file to declare the class properties and methods. You add code in the implementation file to make methods declared in the interface file work. There are two ways to create an Objective-C class in an iOS project

  1. You use one of these templates when you create a new project.
    xcode-proj-templates
  2. You use Xcode’s Objective-C class template.

That’s pretty much all the background knowledge you need to know about Objective-C’s class. Now, you are probably wondering how do you actually create a class, instantiate an object of the class, and use the object? Well you will learn how in due time.

Message Sending

You already know that an object is an instance of a class. One thing you should know about objects is that they accept and respond to messages sent to them.

message_passing

The object receiving the message is called the receiver. The message itself is a method of the receiver. You are probably wondering who sends the message. You do, by writing OC code.

Your OC code can send messages to classes and objects. When you want something to happen, you must send a message to the object or class responsible for the action. When you send a message to a class or object, the message will invoke a method of the class or object. Now, a message can have 0 – n arguments. A message may perform some action or return a value. You can send a message to a class any time but you must instantiate an object before sending it a message.

One such message you can send to an instance of the UITableView control, is:
removeObjectAtIndex:

[book removeObjectAtIndex:indexPath.row];

Sending the removeObjectAtIndex: message to the book object (the receiver) triggers the removeObjectAtIndex: method (the message) and pass in the indexPath.row (an argument).

One way to read above message is like this:

send removeObjectAtIndex: to the books object.

How the book object actually removes an object at the specified row is up to the table object control implementation. The removeObjectAtIndex: message has only one argument. Figure 1 shows how the necessary Objective-C code is executed when a message is sent to an object.

The left side of the figure shows an instance of the UITableView control. The table object has a pointer (first arrow) to its class. The UITableView class has a pointer (second arrow) to code for creating a table, for rearranging a table’s rows, for removing a row, and other table methods.

Figure 1
Figure 1

Remember, a class is a blueprint for instantiating (creating) objects. So the UITableView class in Figure 1 is a blueprint that tells an instance of the UITableView class how to be an object of its kind. Here are the steps illustrated in Figure 1:

Step 1: The object that is the target of the message (table object) is consulted to see what its class is.
Step 2: The class looks through its code and find out where the removeObjectAtIndex: method is.
Step 3: Once it’s found, the method’s code is executed, which deletes the table’s row. The indexPath.row argument determines which row to delete.

Don’t forget this: The user is the one who initiates the message by tapping the red Delete button in the table’s row. That’s pretty much how Objective-C’s message sending works in an iOS application.

Inheritance

Inheritance is nothing more than the process of deriving a new class (subclass) from an existing class (superclass). The subclass inherits all properties and methods of its super class. All Objective-C classes inheritance stems from the NSObject superclass. For example, the UIViewController class is a subclass of the UIResponder class. The UIResponder class in turn is a subclass of the NSObject superclass.

UIViewController-inheritance