The Language of Apple Platforms : Object-Oriented Programming and Objective-C

9/14/2010 4:58:06 PM

To better understand the scope of this hour, take a few minutes to search for Objective-C or object-oriented programming in your favorite online bookstore. You will find quite a few books—lengthy books—on these topics.

To provide you with the information you need to be successful in iOS development, this hour concentrates on fundamentals—the core concepts that are used repeatedly throughout the examples and tutorials in this book. The approach in this hour’ is to introduce you to a programming topic in general terms—then look at how it will be performed when you sit down to write your application. Before we begin, let’s look a bit closer at Objective-C and object-oriented programming.

What Is Object-Oriented Programming?

Most people have an idea of what programming is and have even written a simple program. Everything from setting your TiVo to record a show to configuring a cooking cycle for your microwave is a type of programming. You use data (such as times) and instructions (like “record”) to tell your devices to complete a specific task. This certainly is a long way from developing for the iPhone, but in a way the biggest difference is in the amount of data you can provide and manipulate and the number of different instructions available to you.

Imperative Development

There are two primary development paradigms. First, imperative programming (sometimes called procedural programming) implements a sequence of commands that should be performed. The application follows the sequence and carries out activities as directed. Although there may be branches in the sequence or movement back and forth between some of the steps, the flow is from a starting condition to an ending condition with all the logic to make things “work” sitting in the middle.

The problem with imperative programming is that it lends itself to growing, without structure, into an amorphous blob. Applications gain features when developers tack on bits of code here and there. Frequently, instructions that implement a piece of functionality are repeated over and over wherever something needs to take place. On the other hand, imperative development is something that many people can pick up and do with very little planning.

The Object-Oriented Approach

The other development approach, and what we use in this book, is object-oriented programming (OOP). OOP uses the same types of instructions as imperative development, but structures them in a way that makes your applications easy to maintain and promotes code reuse whenever possible. In OOP, you create objects that hold the data that describes something along with the instructions to manipulate that data. Perhaps an example is in order.

Consider a program that enables you to track reminders. With each reminder, you want to store information about the event that will be taking place—a name, a time to sound an alarm, a location, and any additional miscellaneous notes that you may want to store. In addition, you need to be able to reschedule a reminder’s alarm time or completely cancel an alarm.

In the imperative approach, you have to write the steps necessary to track all the reminders, all the data in the reminders, check every reminder to see whether an alarm should sound, and so on. It’s certainly possible, but just trying to wrap your mind around everything that the application needs to do could cause some serious headaches. An object-oriented approach brings some sanity to the situation.

In an object-oriented model, you could implement a reminder as a single object. The reminder object would know how to store the properties such as the name, location, and so on. It would implement just enough functionality to sound its own alarm and reschedule or cancel its alarm. Writing the code, in fact, would be very similar to writing an imperative program that only has to manage a single reminder. By encapsulating this functionality into an object, however, we can then create multiple copies of the object within an application and have them each fully capable of handling separate reminders. No fuss and no messy code!

By the Way

Most of the tutorials in this book make use of one or two objects, so don’t worry about being overwhelmed with OOP. You’ll see enough to get accustomed to the idea—but we’re not going to go overboard!

Another important facet of OOP is inheritance. Suppose you want to create a special type of reminder for birthdays that includes a list of birthday presents that a person has requested. Instead of tacking this onto the reminder object, you could create an entirely new “birthday reminder” that inherits all of the features and properties of a reminder and then adds in the list of presents and anything else specific to birthdays.

The Terminology of Object-Oriented Development

OOP brings with it a whole range of terminology that you need to get accustomed to seeing in this book (and in Apple’s documentation). The more familiar you are with these terms, the easier it will be to look for solutions to problems and interact with other developers. Let’s establish some basic vocabulary now:

Class: The code, usually consisting of a header/interface file and implementation file, which defines an object and what it can do.

Subclass: A class that builds upon another class, adding additional features. Almost everything you use in iPhone development will be a subclass of something else, inheriting all the properties and capabilities of its parent class.

Superclass/parent class: The class that another class inherits from.

Singleton: A class that is instantiated only once during the lifetime of a program. For example, a class to read your device’s orientation is implemented as a singleton because there is only one sensor that returns tilt information.

Object/instance: A class that has been invoked and is active in your code. Classes are the code that makes an object work, whereas an object is the actual class “in action.” This is also known as an “instance” of a class.

Instantiation: The process of creating an active object from a class.

Instance method: A basic piece of functionality, implemented in a class. For the reminder class, this might be something like setAlarm to set the alarm for a given reminder.

Class method: Similar to an instance method, but applicable to all the objects created from a class. The reminder class, for example, might implement a method called countReminders that provides a count of all the reminder objects that have been created.

Message: When you want to use a method in an object, you send the object a message (the name of the method). This process is also referred to as “calling the method.”

Instance variable: A storage place for a piece of information specific to a class. The name of a reminder, for example, might be stored in an instance variable. All variables in Objective-C have a specific “type” that describes the contents of what they will be holding.

Variable: A storage location for a piece of information. Unlike instance variables, a “normal” variable is only accessible in the method where it is defined.

Parameter: A piece of information that is provided to a method when it is messaged. If you were to send a reminder object the “set alarm” method, you would presumably need to include the time to set. The time, in this case, would be a parameter used with the setAlarm method.

Property: An instance variable that has been configured using special directives to provide easy access from your code.

Did You Know?

You may be wondering, if almost everything in iPhone development is a subclass of something else, is there some sort of master class that “starts” this tree of inheritance? The answer is yes. The NSObject class serves as the starting point for most of the classes you’ll be using on the iPhone. This isn’t something you’ll really need to worry about in the book—just a piece of trivia to think about.

It’s important to know that when you develop on the iPhone, you’re going to be taking advantage of hundreds of classes that Apple has already written for you! Everything from creating onscreen buttons to manipulating dates and writing files is covered by prebuilt classes. You’ll occasionally want to customize some of the functionality in those classes, but you’ll be starting out with a toolbar already overflowing with functionality.

Did You Know?

Confused? Don’t worry! This book introduces these concepts slowly, and you’ll quickly get a feel for how they apply to your projects as you work through several tutorials in the upcoming hours.

What Is Objective-C?

A few years ago, I would have answered this question with “one of the strangest looking languages I’ve ever seen.” Today, I love it (and so will you). Objective-C was created in the 1980s and is an extension of the C language. It adds many additional features to C and, most important, an OOP structure. Objective-C is primarily used for developing Mac and iOS applications and has attracted a devoted group of followers who appreciate its capabilities and syntax.

Objective-C statements are easier to read than other programming languages and often can be deciphered just by looking at them. For example, consider the following line that compares whether the contents of a variable called myName is equal to John:

[myName isEqualToString:@"John"]
It doesn’t take a very large mental leap to see what is going on in the code snippet. In traditional C, this might be written as follows:
The C statement is a bit shorter, but does little to convey what the code is actually doing.


Objective-C is case sensitive! If a program is failing, make sure you aren’t mixing case somewhere in the code!

Because Objective-C is implemented as a layer on top of C, it is still fully compatible with code that is written entirely in C. For the most part, this isn’t something that you should concern yourself with, but unfortunately, Apple has left a bit of “cruft” in their iOS SDK that relies on C-language syntax. You’ll encounter this infrequently, and it isn’t difficult to code with when it occurs, but it does take away from the elegance of Objective-C just a little.

Now that you have an idea of what OOP and Objective-C are, let’s take a look at how you’ll be using them over the course of this book.

Video tutorials
- How To Install Windows 8

- How To Install Windows Server 2012

- How To Install Windows Server 2012 On VirtualBox

- How To Disable Windows 8 Metro UI

- How To Install Windows Store Apps From Windows 8 Classic Desktop

- How To Disable Windows Update in Windows 8

- How To Disable Windows 8 Metro UI

- How To Add Widgets To Windows 8 Lock Screen

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010
programming4us programming4us
Top 10
Free Mobile And Desktop Apps For Accessing Restricted Websites
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
Popular Tags
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone