Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [29]
#import int main (int argc, const char * argv[]) { @autoreleasepool { NSDate *now = [[NSDate alloc] init]; 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); NSCalendar *cal = [NSCalendar currentCalendar]; NSUInteger day = [cal ordinalityOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:now]; NSLog(@"This is day %lu of the month", day); } return 0; } The method’s name is ordinalityOfUnit:inUnit:forDate:, and it takes three arguments. You can tell because the method name includes three colons. Notice that I split that message send into three lines. This is fine; the compiler does not mind the extra whitespace. Objective-C programmers typically line up the colons so that it is easy to tell the parts of the method name from the arguments. (Xcode should do this for you: every time you start a new line, the previous line should indent properly. If that isn’t happening, check your Xcode preferences for indention.) The first and second arguments of this method are the constants NSDayCalendarUnit and NSMonthCalendarUnit. These constants are defined in the NSCalendar class. They tell the method that you want to know what day it is within the month. The third argument is the date you want to know about. If you wanted to know the hour of the year instead of the day of the month, you’d use these constants: NSUInteger hour = [cal ordinalityOfUnit:NSHourCalendarUnit inUnit:NSYearCalendarUnit forDate:now]; Sending messages to nil In most object-oriented languages, sending a message to nil is not allowed. As a result, you have to check for non-nil-ness before accessing an object. So you see this sort of thing a lot: if (fido != nil) { [fido goGetTheNewspaper]; } When Objective-C was designed, it was decided that sending a message to nil would be OK; it just wouldn’t do anything at all. Thus, this code is completely legal: Dog *fido = nil; [fido goGetTheNewspaper]; Important thing #1: If you are sending messages and nothing is happening, make sure you aren’t sending messages to a pointer that has been set to nil. Important thing #2: If you send a message to nil, the return value doesn’t mean anything. Dog *fido = nil; Newspaper *daily = [fido goGetTheNewspaper]; In this case, daily will be zero. (In general, if you expect a number or a pointer as a result, sending a message to nil will return zero. However, for other types like structs, you will get strange and unexpected return values.) Challenge Write a Foundation Command Line Tool that tells you if it is currently daylight savings time. 14 NSString NSString *lament = @"Why me!?"; In your TimeAfterTime project, you typed in this code: NSLog(@"The date is %@", now); NSLog() is an Objective-C function (not a method!) that works a lot like printf(). In NSLog(), however, the format string is actually an instance of NSString. Instances of NSString can also be created programmatically using the stringWithFormat: class method: NSString *x = [NSString stringWithFormat:@"The best number is %d", 5]; To get the number
Nearly all object-oriented languages have the idea of nil, the pointer to no object. In Objective-C, nil is the zero pointer (same as NULL, which was discussed in Chapter 8).
On your Mac, there is a class NSTimeZone. Among its many methods are the class method systemTimeZone, which returns the time zone of your computer (as an instance of NSTimeZone), and the instance method isDaylightSavingTime, which returns YES if the time zone is currently in daylight savings time.
NSString is another class like NSDate. Instances of NSString hold character strings. In code, you can create a literal instance of NSString like this;