Welcome to Class Files
Most, if not all, of the code you write for an iOS app is contained in what are called class files. A class is an object-oriented programming term and is vital to understanding most of what goes on in iOS SDK programming.
A class is code that defines the characteristics of an object.
You’ve worked with tons of objects in JavaScript—whether they be pure
language objects, such as the Date
object, or DOM objects that
represent HTML elements on the page—so you should already be familiar with
the concept of an object having properties (which can hold data) and
methods (which perform actions). In fact, when you write the code for a
JavaScript object constructor function, you are doing essentially the same
job that you will be doing when you write the code for an Objective-C
class definition. It’s just that the format and environment for the
Objective-C variety is radically different from the JavaScript
constructor. To help you visualize the differences between the JavaScript
object constructor and Objective-C class definitions, I’ll show you a
variation on a venerable JavaScript object constructor example, which I
will then rewrite in Objective-C (you can even test it in Workbench by the
end of the chapter). Both examples focus on using the constructor or class
as a way to obtain object instances.
The JavaScript Way
I’ll begin with a variation of an “old chestnut” example, one that defines an object that represents an automobile. I’ll add a function to the old standard ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access