Online Book Reader

Home Category

Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [28]

By Root 461 0
message send

Objects in memory


Figure 12.3 is an object diagram. This diagram shows two NSDate instances on the heap. The two variables now and later are part of the frame for the function main(). They point to the NSDate objects, as shown by the arrows.

Figure 12.3 Object diagram for TimeAfterTime

You’ve only seen one class thus far: NSDate. There are, in fact, hundreds of classes that come with iOS and Mac OS X. We’ll work with some of the more common ones in the coming chapters.

id


When declaring a pointer to hold on to an object, most of the time you specify the class of the object that the pointer will refer to:

NSDate *expiration;

However, often you need a way to create a pointer without knowing exactly what kind of object the pointer will refer to. For this case, we use the type id to mean “a pointer to some kind of Objective-C object” Here is what it looks like when you use it:

id delegate;

Notice that there is no asterisk in this declaration. id implies the asterisk.

The ideas of classes, objects, messages, and methods can be difficult to get your head around at the beginning. Don’t worry if you're feeling a little uncertain about objects. This is just the beginning. You’ll be using these patterns over and over again, and they will make more sense each time you do.

Challenge


Use two instances of NSDate to figure out how many seconds you have been alive. Hint: here is how you create a new date object from the year, month, etc.:

NSDateComponents *comps = [[NSDateComponents alloc] init];

[comps setYear:1969];

[comps setMonth:4];

[comps setDay:30];

[comps setHour:13];

[comps setMinute:10];

[comps setSecond:0];

NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *dateOfBirth = [g dateFromComponents:comps];

To get the number of seconds between two instances of NSDate, use the method timeIntervalSinceDate:.

double d = [laterDate timeIntervalSinceDate:earlierDate];

13

More Messages


In the last chapter, you sent a few messages. Let’s look again at the lines where you sent those messages and triggered the corresponding methods:

NSDate *now = [NSDate date];

We say that the date method is a class method. That is, you cause the method to execute by sending a message to the NSDate class. The date method returns a pointer to an instance of NSDate.

double seconds = [now timeIntervalSince1970];

We say that timeIntervalSince1970 is an instance method. You cause the method to execute by sending a message to an instance of the class. The timeIntervalSince1970 method returns a double.

NSDate *later = [now dateByAddingTimeInterval:100000];

dateByAddingTimeInterval: is another instance method. This method takes one argument. You can determine this by the colon in the method name. This method also returns a pointer to an instance of NSDate.

Nesting message sends


There is a class method alloc that returns a pointer to a new object that needs to be initialized. That pointer is then used to send the new object the message init. Using alloc and init is the most common way to create objects in Objective-C. It is good practice (and the only Apple-approved way) to send both of these messages in one line of code by nesting the message sends:

[[NSDate alloc] init];

The system will execute the messages on the inside first and then the messages that contain them. So, alloc is sent to the NSDate class, and the result of that (a pointer to the newly-created instance) is then sent init.

init returns a pointer to the new object (which is nearly always the pointer that came out of the alloc method), so we can use what’s returned from init for the assignment. Try out these nested messages in your code by changing the line:

NSDate *now = [NSDate date];

to

NSDate *now = [[NSDate alloc] init];

Multiple arguments


Some methods take several arguments. For example, an NSDate object doesn’t know what day of the month it is. If you needed this information, you would use an NSCalendar object. The NSCalendar

Return Main Page Previous Page Next Page

®Online Book Reader