Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [27]
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
Files containing Objective-C code are typically given the suffix .m. Find and open main.m and type in these two lines of code:
#import int main (int argc, const char * argv[]) { @autoreleasepool { NSDate *now = [NSDate date]; NSLog(@"The new date lives at %p", now); } return 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 \n at the end. Build and run the program. You should see something like: 2011-08-05 11:53:54.366 TimeAfterTime[4862:707] The new date lives at 0x100114dc0 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: #import int main (int argc, const char * argv[]) { @autoreleasepool { NSDate *now = [NSDate date]; NSLog(@"The date is %@", now); } return 0; } Now you should see something like: The date is 2011-08-05 16:09:14 +0000 Message anatomy a pointer to the object that is receiving the message the name of the method to be triggered A message send (like a function call) can also have arguments. Let’s look at an example. NSDate objects represent a particular date and time. An instance of NSDate can tell you the difference (in seconds) between the date/time it represents and 12:00AM (GMT) on Jan 1, 1970. Ask yours this question by sending the message timeIntervalSince1970 to the NSDate object pointed to by now. #import int main (int argc, const char * argv[]) { @autoreleasepool { NSDate *now = [NSDate date]; NSLog(@"The date is %@", now); double seconds = [now timeIntervalSince1970]; NSLog(@"It has been %f seconds since the start of 1970.", seconds); } return 0; } Now say you want a new date object – one that is 100,000 seconds later from the one you already have. The NSDate class has a method called dateByAddingTimeInterval:. You can send this message to the original date object to get the new date object. This method takes an argument: the number of seconds to add. Use it to create a new date object in your main() function: #import int main (int argc, const char * argv[]) { @autoreleasepool { NSDate *now = [NSDate date]; NSLog(@"The date is %@", now); double seconds = [now timeIntervalSince1970]; NSLog(@"It has been %f seconds since the start of 1970.", seconds); NSDate *later = [now dateByAddingTimeInterval:100000]; NSLog(@"In 100,000 seconds it will be %@", later); } return 0; } In the message send [now dateByAddingTimeInterval:100000], now is a pointer to the object that is receiving the message (also known as “the receiver”) dateByAddingTimeInterval: is the method name (also known as “the selector”) 100000 is the only argument Figure 12.2 A
A message send is always surrounded by square brackets, and it always has at least two parts: