This has been by far the hardest challenge. If the answer had not been practically given, I’m certain I wouldn’t have gotten it. It makes me worry about whether I have a firm enough grasp on the material to continue.
I know this part was provided, but can someone explain what’s going on here? I was able to get the challenge, but were we expected to know/understand the below, or is that why they provided it for us?
[quote=“RahulLanka”]I know this part was provided, but can someone explain what’s going on here? I was able to get the challenge, but were we expected to know/understand the below, or is that why they provided it for us?
Here’s my take: The first line is initializing to the current date by the Gregorian Calendar? Why is it necessary to use the ‘WithCalendarIdentifier:NSGregorianCalendar’ part?
And the second is just sending the address of the previously set date to the ‘dateOfBirth’ pointer? Why can’t you just use ‘comps’ in ‘secondsSinceEarlierDate’?
I wasn’t confused when I immediately read the challenge. I actually thought I knew exactly what to do. It wasn’t until I saw all the code he gave us.
In fact, I’ve noticed throughout the book there are more and more examples where we get new classes from the foundation and their hardly ever explained prior to being used. Lot’s of research is required. I tried this challenge a couple a times. I certainly did not get it on the first try but can’t give up. It’s interesting how there are several solutions to the same answer. Neat.
@autoreleasepool {
// Chapter 14 More Messages
// Use two instances of NSDate to figure out how many seconds you have been alive.
NSDate *actualDate = [[NSDate alloc] init];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1969];
[comps setMonth:4];
[comps setDay:30];
[comps setHour:13];
[comps setMinute:10];
[comps setSecond:0];
NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *dateOfBirth = [g dateFromComponents:comps];
double secondsSinceEarlierDate = [actualDate timeIntervalSinceDate:dateOfBirth];
NSLog(@"Date of Birth %@", dateOfBirth);
NSLog(@"It has been %f seconds since my Bday", secondsSinceEarlierDate);
}
return 0;
}
[/code]
Output:
2014-08-19 08:33:38.622 TimeAfterTime[531:303] Date of Birth 1969-04-30 17:10:00 +0000
2014-08-19 08:33:38.622 TimeAfterTime[531:303] It has been 1429646018.617459 seconds since my Bday
Program ended with exit code: 0
1.- You are initializing an instance of NSDateComponents with your birth date (this class means nothing until populated, think of it as a place holder for the various date components)
2.- You are initializing a NSCalendar instance to a gregorian calendar.
3.- You are initializing a NSDate instance to the birth date by populating the NSCalendar instance with the values from the NSDateComponents instance.
4.- Lastly, you arrive at your answer via the timeIntervalSinceDate method, ran against the NSDate instance above to return the time interval (the seconds). You are holding the result in a ‘double’ and logging the output with NSLog.
At this stage in the book it’s all about becoming familiar with the framework. I find that having the apple reference doc open side by side with Xcode is invaluable. You get to read about the Classes and the various methods available, etc.