13
Objects

In this chapter, you will write your first Objective-C program. This program will be a command-line tool like the ones you have written so far, but it will be written in Objective-C.

In the early 1980’s, Brad Cox and Tom Love created the Objective-C language. For objects, they built upon the idea of structs allocated on the heap and added a message-sending syntax.

As you move from C programming to Objective-C programming, you are entering the world of objects and object-oriented programming. Be prepared to encounter new concepts and to be patient. You will be using these patterns again and again, and they will become clear with time and practice.

Objects

An object is similar to a struct (such as the struct Person you created in Chapter 11). Like a struct, an object can contain several pieces of related data. In a struct, we called them members. In an object, we call them instance variables (or you might hear ivars).

An object differs from a struct in that an object can also have its own functions that act on the data it contains. These functions are called methods.

Classes

A class describes a certain type of object by listing the instance variables and methods that object will have. A class can describe an object that represents

  • a concept, like a date, a string, or a set

  • something in the real world, like a person, a location, or a checking account

A class defines a kind of object. It also produces objects of that kind. You can think of a class as both blueprint and factory.

In Chapter 18, you will rewrite the BMI-calculating program using objects instead of structs. You are going to create a class named Person. The objects that it produces will be instances of the Person class. These instances will have instance variables for height and weight and a method for calculating the BMI.

Figure 13.1  A Person class and two Person instances

A Person class and two Person instances

A note about our object diagrams: Classes, like the Person class, are diagrammed with a dashed border. Instances are drawn with solid borders. This is a common diagramming convention for distinguishing between classes and instances of a class.

At this point in this chapter, we are going to switch from theory to practice. Do not worry if objects, classes, instances, and methods do not make perfect sense yet. Practice will help.

Instead of starting off by writing a new custom class, you are going to create instances of a class that Apple has provided. This class is named NSDate. An instance of NSDate represents a point in time. You can think of it as a timestamp. You will also be using methods from the NSDate class.

Creating your first object

Create a new Command Line Tool project named TimeAfterTime. Make its type Foundation – not C like with your past projects (Figure 13.2).

Figure 13.2  Creating a Foundation command-line tool

Creating a Foundation command-line tool

Files containing Objective-C code are given the suffix .m. Find and open main.m.

At the top of this file, find the line that reads

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

When Xcode created your project, it imported the Foundation framework for you. A framework is a set of related classes, functions, constants, and types. The Foundation framework contains fundamental classes that are used in all iOS apps and OS X applications. The NSDate class is in the Foundation framework.

What is the difference between #import and #include? #import is faster and more efficient. When the compiler sees the #include directive, it makes a dumb copy-and-paste of the contents of the file to include. When the compiler sees the #import directive, it first checks to see if another file may have already imported or included the file.

In main.m, add the following line of code:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​n​o​w​ ​=​ ​[​N​S​D​a​t​e​ ​d​a​t​e​]​;​

 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

On the left side of the assignment operator (=), you have a variable named now. You can tell from the * that this variable is a pointer. This pointer holds the address in memory where the instance of NSDate lives.

The code on the right side returns the address of an instance of NSDate. This code is known as a message send, and you will learn about messages in the next section. First, add the following line that writes the address of the NSDate instance using the function NSLog().

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​n​o​w​ ​=​ ​[​N​S​D​a​t​e​ ​d​a​t​e​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​T​h​i​s​ ​N​S​D​a​t​e​ ​o​b​j​e​c​t​ ​l​i​v​e​s​ ​a​t​ ​%​p​"​,​ ​n​o​w​)​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

NSLog() is a function in the Foundation framework that is a lot like printf(). It accepts a format string and can have replaceable tokens.

Build and run the program. You should see something like:

2​0​1​3​-​0​8​-​0​5​ ​1​1​:​5​3​:​5​4​.​3​6​6​ ​T​i​m​e​A​f​t​e​r​T​i​m​e​[​4​8​6​2​:​7​0​7​]​ ​T​h​i​s​ ​N​S​D​a​t​e​ ​o​b​j​e​c​t​ ​l​i​v​e​s​ ​a​t​ ​0​x​1​0​0​1​1​6​2​4​0​

Unlike printf(), NSLog() prefaces its output with the date, time, program name, and process ID. From now on, when we show output from NSLog(), we will skip this data – the page is just too narrow.

T​h​i​s​ ​N​S​D​a​t​e​ ​o​b​j​e​c​t​ ​l​i​v​e​s​ ​a​t​ ​0​x​1​0​0​1​1​6​2​4​0​

You have created an instance of NSDate, and it lives at the address stored in now. To understand how this happened, you need to know about methods and messages.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset