Advertisement
leonarth

Challenge

Aug 30th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Challenge
  2.  
  3. Use two instances of NSDate to figure out how many seconds you have been alive.
  4. First, NSDate has an instance method timeIntervalSinceDate:. This method takes one argument โ€“ another instance of NSDate. It returns the number of seconds between the NSDate that received the message and the NSDate that was passed in as the argument.
  5. It looks something like this:
  6. double secondsSinceEarlierDate = [laterDate timeIntervalSinceDate:earlierDate];
  7. Second, you will need to create a new date object that is set to a given year, month, etc. You will do this with the help of an NSDateComponents object and an NSCalendar object. Here is an example:โ€
  8.  
  9. Excerpt From: Ward, Mikey. โ€œObjective-C Programming: The Big Nerd Ranch Guide, 2/e (Big Nerd Ranch Guides).โ€ iBooks. */
  10.  
  11.         NSDateComponents *comps = [[NSDateComponents alloc] init];
  12.         [comps setDay:20];
  13.         [comps setMonth:01];
  14.         [comps setYear:1985];
  15.         NSDate *now = [NSDate date];
  16.         NSCalendar *local = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  17.         NSDate *birthday = [local dateFromComponents:comps];
  18.        
  19.         double difference = [birthday timeIntervalSinceDate:now];
  20.        
  21.         NSLog(@"I've been living for %f time.", difference);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement