I knew I was in for a hard time when the book wished me “Good luck!”. I’m surprised I got it. Nothing original, just like most of you but here it is…[code]#import <Foundation/Foundation.h>
int main(int argc, const char * argv)
NSDate now = [NSDate date]; / now */
/* here we create a new date object 'comps' that's set to a given year, month, day etc. that equates to the time I was born */ NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setYear:1951]; [comps setMonth:7]; [comps setDay:23]; [comps setHour:23]; [comps setMinute:59]; [comps setSecond:59]; /* here NSCalendar provides the implementation of the Gregorian Calendar format */ NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; /* here the code creates a date object (*dateOfBirth) to represent 23:59:59 on 23 July 1951, for a given calendar (gregorian). */ NSDate *dateOfBirth = [g dateFromComponents:comps]; /* the instance method 'timeIntervalSinceDate: returns the number of seconds between dateOfBirth & now */ double secondsSinceBirthdate = [now timeIntervalSinceDate:dateOfBirth]; NSLog(@"\n\nI've been alive for %.0f seconds.\n\n", secondsSinceBirthdate); /* prints out the number of seconds I've been alive */ } return 0;
[/code] Output:[quote]I’ve been alive for 1997611323 seconds.
That’s a lot of seconds!