Archive for November, 2011

Object Oreinted Programming for IOS

26 November 2011 Leave a comment



Michael Khan

Department of Electronic and Computer Engineering

University of Limerick

Limerick, Ireland


With the recent innovations in hardware mobile devices have become increasingly powerful and therefore software options for these mobile devices must also advance. Only a few years ago most mobile phones, mp3 player and other mobile devices had very low processing power, small low-resolution black and white screens and limited sound capabilities. The only software used was a small operating system with very little functionality and almost always coded in Assembly language or C.  With the new modern devices there is a lot more functionality to be explored and with the amount of people who own devices like the iPhone and Android phones a vast market for software for these devices was created. To create software that can use the functionalities of these new devices operating systems like Android and the Apple iOS have incorporated Object Oriented-Programming capabilities for third party developers to create their own applications or “Apps” as the have become so widely known. In this paper I will discuss how Object-Oriented Programming (OOP) is used to develop applications for the iPhone and other iOS devices.


The mobile software market share has over 6% of the overall software market share, which is significant since it was just over 1% in October 2 years ago. Apple’s iOS is the leader in the mobile operating systems with over 54% of the market share. This is why iOS development has become so widespread and why today you can find over 425000 applications on the App Store.

The official concept of objects in programming was introduced in the 1960’s in Simula 67. As the name suggests Simula 67 was a programming language designed for discreet event simulation. Simula 67 introduced the concept of classes, subclasses and instances of objects; the language also used automatic garbage collection. Object-oriented programming became the dominant programming methodology in the early to mid 1990’s when languages supporting the method became readily available, including C++ and Delphi. Its dominance was further increased with the rising popularity of graphical user interfaces (GUI). Mac OS X and iOS have a dynamic GUI library found in the Cocoa framework, which was written in Objective-C.

In mid 2008 with the launch of the iPhone 3G Apple also announced that they would be making releasing a software development kit (SDK) so that 3rd parties could create their own applications and sell them on their new App Store. In this software development kit Apple decided to use the Objective-C programming language for the development of iOS applications. Brad Cox and Tom Love created the Objective-C programming language in the early 1980s. They added on Object-Oriented capabilities from the Smalltalk language and added it on to the C language, C was chosen because it was such a widely used language and familiar to most people. Apple adopted Objective-C when they acquired NeXT in 1996, NeXT was Steve Job’s company that licensed Objective-C and boosted its popularity. Since then Apple has made Objective-C the official language for development on Mac OS X operating system and its derived version for portable devices iOS.

In the rest of this paper I will explain different parts of object-oriented programming and show how they apply to programming in Objective-C for iOS devices.


Inheritance is quite possibly the most Important and recognized of all attributes of object-oriented programming. I like to think of inheritance like this: the easiest way to describe something new is to use something that is already know in your description, for example if I want to describe a baseball it is easier if the person that I am trying to explain it to already knows what a ball is. The same holds true to programming, if you want to define a new of object it is easier it is easier if you can start from the definition of an existing object. Objective-C and other objective-oriented programming languages allow you to base the definition of a new class on the definition of an existing one. The new class then is known as a subclass and the class it derived from is its superclass. In Objective-C a superclass can have multiple subclasses but a subclass can only inherit from one superclass

Figure 1 An Inheritance hierarchy

In iOS programming the NSObject class is the root of most class hierarchy. NSObject objects only inherit a basic interface to the run time and the capacity to behave as an Objective-C object.

In Objective-C inheritance is declared in the header file (.h file) using this syntax:

In this example CalculatorBrain is the subclass and NSObject the superclass, the @interface tag is how classes are defined in the header file.

Inheritance in Objective-C is very similar to Java. You can overwrite methods of the superclass by having the same name on the method in the subclass. One difference is that you need to make the constructor of the superclass return the special type id, which I will talk about later if you want to be able to use it to create objects of its subclasses.


Polymorphism is the ability of different objects to reply, each in its on way to the same message. It originates from the fact that each class has its own namespace meaning that the names allocated inside a class do not clash with names allocated anywhere outside it. This is true for both instance variables and methods in the class.

An example that shows Polymorphism is an Animal class that has 2 subclasses called Dog and Cat. A method talk() is specified on the animal class and it prints the string “An animal does not talk” on the screen. Both Cat Dog classes inherits this method from animal but they do not print the same string they instead prints ”Meow meow” and “Woof woof” respectively because that’s the sound those animal make. Polymorphism is used here so that the Animal, Cat and Dog classes can have a method called talk().

Overloading and Polymorphism refer essentially to the same thing, but from slightly different points of view. Polymorphism points out that a several classes can have a method with the same name. Parameter overloading takes the view that a method can have different effects depending on the parameter that are passed to it. Operator overloading is similar, it allows an operator in the language (like + or ==) to be turned into a method and have completely different functionality depending on the type of object it is applied to. An example is using the ‘+’ operator to add an integer into an array or using the ‘+’ operator to concatenate strings. Operator overloading is common practice in C++ but it is not considered good programming practice by many programmers, as it can be confusing to know what meaning the operator is taking in certain parts of the code. Objective-C supports polymorphism of methods names, but not parameters or operator overloading.

Even without supporting parameter or operator overloading Objective-C is still considered one of the most polymorphic of all compiled languages because of the dynamic nature of its method lookup. Dynamic binding is the biggest difference between Objective-C and other object-oriented programming languages like Java and C++. This is where the type id is very important.

id is a pointer to an object of unknown type.  Both of the above pieces of code mean the same thing at runtime since in iOS programming using Objective-C the decision about what code to run on a message send happens at run time. So in the above example ‘s’ is created as a pointer to an object but the decision of what kind of object ‘s’ will be is not done until runtime. The top piece of code uses static typing which is only different at compile time, in this case the compiler can help the programmer look for problems in the code. An example of how static typing can be helpful is that if you try to send ‘s’ a message that can not be used on NSString object the compiler will warn you at compile time that it can not be done but the decision as to what code will be executed is still done at runtime no matter if you use NSString or id.

To accommodate this dynamic binding Objective-C has a feature called Introspection.  Introspection allows the programmer to send messages to and object to check if it is a certain class or if it responds to a given method. There are 3 introspection methods available in the NSObject class, which is inherited into most classes in iOS development. These methods are: isKindOfClass which returns whether an object is that kind of class or (including inheritance), isMemberOfClass which returns whether an object is that kind of class (without inheritance) and respondsToSelector whether an object responds to a given method. The programmer can use these methods; to check and decide what methods can be used with an object.

In the above code example the programmer first checks that ‘obj’ is of class NSString with the if statement before then casting ‘obj’ to be an NSString object. The Objective-C compiler will not provide warning when casting is used therefore it should only be used with a check like in the above example or when you are absolutely sure that the type the object is being casted is its actual type otherwise the program will crash at runtime with no warning from the compiler.


Encapsulation is another important concept of object-oriented programming. It allows the programmer to select what kind of access they give of the value of variable to the users of their class. Encapsulation is important so that a user can’t set a value to a prohibited value. A trivial example is when performing division, if a user changes the value of an integer ‘d’ to ‘0’ and the integer ‘d’ is then used as the divisor in a calculation you will get an error. This is a simple example of a use of encapsulation but there are many reasons why you would want to protect the value of your variables with encapsulation.

Objective-C uses 3 keywords for encapsulations, they are: @private, @protected and @public. @private means that the instance variable can only be accessed inside its class. @protected means that the variables can be accessed from inside its own class and from its subclasses only. @public means that the variable can be accessed form anywhere. In Objective-C, by default instance variables are set to @protected.

In the above example ‘foo’ and ‘bar’ are both protected access,  ‘foo’ is protected by default, as there is no keyword to specify its access above it in the code. ‘forum’ and ‘apology’ can both be public accessed as all variables declared under the keyword have that type of access. It is recommended to set all instance variables to private to avoid problems when programming.

Because most instance variables are set to private there has to be a mechanism to allow for access to these variables in an other more secure way. For this setter and getter methods are used. Setter and getter methods are used in most programming languages. The big difference in Objective-C is that the compiler can generate the code for the getter and setter methods for you.  The keyword @property declares the getter and setter methods in the header file and the @synthesize keyword synthesizes the setter and getter methods on the ‘.m’ file.

            Header file (.h)

Implementation (.m)

This example shows the use of @property and @synthesize.

The @property and @synthesize can be changed to allow read only access and the setter and the getter can be overwritten to provide extra security to the setter method. An example where you would use this is in the previous example of dividing by zero. You could have the setter method check when the user tries to set the variable that it is not set to ‘0’ so you don’t get an error.


This paper presented many of the object-oriented aspects used for programming in iOS. These aspects include Inheritance, Polymorphism and Encapsulation. Inheritance is the ability of starting a class from the description of an other. Polymorphism is the ability of methods in different classes to have the same name and different implementations. Encapsulation is the ability to stop the user from changing the value of variables that could be set out of its range.

Finally I should note that there is a new iOS coming out in the next month, iOS 5 there are some new features added to but the object-oriented programming aspects of it should remain the same.


  1. Market share for mobile, browsers, operating systems and search engines | NetMarketShare. 2011. Market share for mobile, browsers, operating systems and search engines | NetMarketShare. [ONLINE] Available at: [Accessed 12 October 2011].
  2. Apple – iPhone 4S – See apps and games from the App Store.. 2011. Apple – iPhone 4S – See apps and games from the App Store.. [ONLINE] Available at: [Accessed 12 October 2011].
  3. Objective-C 2.0 Essentials – Techotopia. 2011. Objective-C 2.0 Essentials – Techotopia. [ONLINE] Available at: [Accessed 12 October 2011].
  4. Downloads (2010-2011 Fall) | CS 193P iPhone Application Development. 2011. Downloads (2010-2011 Fall) | CS 193P iPhone Application Development. [ONLINE] Available at: [Accessed 12 October 2011].
  5. Apple Developer. 2011. Apple Developer. [ONLINE] Available at: [Accessed 12 October 2011].
Categories: Uncategorized