Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Challenge
- Use two instances of NSDate to figure out how many seconds you have been alive.
- 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.
- It looks something like this:
- double secondsSinceEarlierDate = [laterDate timeIntervalSinceDate:earlierDate];
- 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:โ
- Excerpt From: Ward, Mikey. โObjective-C Programming: The Big Nerd Ranch Guide, 2/e (Big Nerd Ranch Guides).โ iBooks. */
- NSDateComponents *comps = [[NSDateComponents alloc] init];
- [comps setDay:20];
- [comps setMonth:01];
- [comps setYear:1985];
- NSDate *now = [NSDate date];
- NSCalendar *local = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
- NSDate *birthday = [local dateFromComponents:comps];
- double difference = [birthday timeIntervalSinceDate:now];
- NSLog(@"I've been living for %f time.", difference);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement