12
Objects

Many computer languages have the idea of objects. An object is like a structure in that it holds data. However, unlike a structure, an object also contains a set of functions that act upon that data. To trigger one of these functions, you send a message to the object. To use the correct word, a function that is triggered by a message is known as a method.

In the early 1980’s, Brad Cox and Tom Love decided to add object-oriented ideas to the C language. For objects, they built upon the idea of structs allocated on the heap and added a message-sending syntax. The result was the language Objective-C.

Objects are very chatty by nature. They do work and send and receive messages about the work they are doing. A complex Objective-C program can have hundreds of objects in memory all at once, doing work and sending messages to each other.

A class describes a particular type of object. This description includes methods and instance variables where an object of this type stores its data. You ask a class to create an object of its type for you on the heap. We say that the resulting object is an instance of that class.

For example, an iPhone ships with many classes, one of which is CLLocation. You can ask the CLLocation class to create an instance of CLLocation. Inside this CLLocation object are several instance variables that hold location data, like longitude, latitude, and altitude. The object also has several methods. For example, you can ask one instance of CLLocation how far it is from another CLLocation object.

Creating and using your first object

Now you’re going to create your first Objective-C program. Create a new project: a Command Line Tool, but instead of C, make its type Foundation. Name it TimeAfterTime.

Figure 12.1  Creating a Foundation command-line tool

Creating a Foundation command-line tool

Files containing Objective-C code are typically given the suffix .m. Find and open main.m and type in these two lines 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​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​T​h​e​ ​n​e​w​ ​d​a​t​e​ ​l​i​v​e​s​ ​a​t​ ​%​p​"​,​ ​n​o​w​)​;​

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

Voilà! Your first message send. You sent the message date to the NSDate class. The date method asks the NSDate class to create an instance of NSDate, initialize it to the current date/time, and return the address where the new object starts. You then stored the returned address in the variable now. This variable is a pointer to an NSDate object.

NSLog() is an Objective-C function not unlike printf(); it takes a format string, replaces % tokens with actual values, and writes the result to the console. However, its format string always begins with an @, and it does not require a at the end.

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

2​0​1​1​-​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​e​ ​n​e​w​ ​d​a​t​e​ ​l​i​v​e​s​ ​a​t​ ​0​x​1​0​0​1​1​4​d​c​0​

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

In NSLog(), %p printed out the location of the object. To print out something more date-like, you can use %@, which asks the object to describe itself as a string:

#​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​e​ ​d​a​t​e​ ​i​s​ ​%​@​"​,​ ​n​o​w​)​;​

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

Now you should see something like:

T​h​e​ ​d​a​t​e​ ​i​s​ ​2​0​1​1​-​0​8​-​0​5​ ​1​6​:​0​9​:​1​4​ ​+​0​0​0​0​
..................Content has been hidden....................

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