Object Oriented Programming, Project Organization and Code Documentation
Most good programmers do programming not because they expect to get paid or adulation by the public, but because it is fun to program.
- Linus Torvalds
If you read from the very beginning of the book and have worked through all the projects, you've gone pretty far. By now, you should be able to build a table-based iOS app, which works on both iPhone and iPad, using Interface Builder. We'll further enhance the FoodPin app and add more features. However, we will dive deeper into iOS app development and learn other APIs. I want to introduce you the basics of Object Oriented Programming and teach you how to write better code.
Don't be scared by the term "Object Oriented Programming" or OOP in short. It's not a new kind of programming language, but a programming concept. While some programming books start out by introducing the OOP concept, I intentionally left it out when I began writing this book. I want to keep things interesting and show you how to create an app. I don't want to scare you away from building apps, just because of a technical term or concept. Having said that, I think it's time to discuss OOP. After going through 10 chapters and you're still reading the book, I believe you're determined to learn iOS programming. And, I believe you really want to take your programming skills to the next level to become a professional developer.
Okay, let's get started.
The Basic Theory of Object Oriented Programming
Like Objective-C, Swift is known as an Object Oriented Programming (OOP) language. OOP is a way of constructing software application composed of objects. In other words, the code written in an app in some ways deals with objects of some kinds. The
UITableView objects that you have used are some sample objects that come with the iOS SDK. Not only can you use the built-in objects, you have already created your own objects in the project, such as
First, why OOP? One important reason is that we want to decompose complex software into smaller pieces (or building block) which are easier to develop and manage. Here, the smaller pieces are the objects. Each object has its own responsibility and objects coordinate with each other in order to make the software work. That is the basic concept of OOP.
Take the Hello World app, that we've built at the very beginning, as an example. The
UIViewController object is responsible for controlling the view of the app and its view object is used for holding the Hello World button. The
UIButton (i.e. Hello World button) object implements a standard iOS button on the touch screen and listens to any touch events. On the other hand, the
UIAlertController object displays an alert message to a user. After all, these objects work together to create the Hello World app.
In Object Oriented Programming, an object shares two characteristics: properties and functionalities. Let's consider a real world object – Car. A car has its own color, model, top speed, manufacturer, etc. These are the properties of a car. In terms of functionalities, a car should provide basic functions, such as accelerate, brake, steering, etc.
Software objects are conceptually similar to real-world objects. If we go back to the iOS world, let's take a look at the properties and functionalities of the
UIButton object in the Hello World app:
- Properties – Background, size, color, and font are examples of the
- Functionalities – When the button is tapped, it recognizes the tap event. The ability to detect a touch is one of the many functions of
In earlier chapters, you always come across the term method. In Swift, we create methods to provide the functionalities of an object. Usually, a method corresponds to a particular function of an object.
Note: You may wonder what is the difference between functions and methods? In fact, they are functionally the same. Functions defined in a class are known as methods.To access the full version of the book, please get the full copy here. You will also be able to access the full source code of the project.