This WAS a challenge!

Hello everyone.

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!


Really enjoyed this one too!
It helped me solidify things like class vs instance methods and the importance of good variable names, and so on. Then it was nice to see a case of using tons of given methods for a Foundation class.

Really quite tough but so worthwhile.

I went over your solution and it’s also like what I had in mind. To be honest, what I found most difficult about the challenge is the instructions were a bit confusing (I think). First, it talks about using the instance method timeIntervalSinceDate, and then it mentions creating the new date object to represent my birthday. I was trying to figure out how to get the answer using the methods in this order! haha.

I have the second edition of the book.


I must agree. The challenge ended up being fairly simple at its core. The way they described the general approach to the problem was mixed up. First step, last step, seconds step. haha

Heres what I did by the way:


[code]NSDate *currentDate = [[NSDate alloc] init];

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1989];
[comps setMonth:6];
[comps setDay:15];
[comps setHour:12];
[comps setMinute:16];
[comps setSecond:0];

NSCalendar *gregCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *birthDate = [gregCal dateFromComponents:comps];

double diffSeconds = [currentDate timeIntervalSinceDate:birthDate];

NSLog(@“The number of seconds since I was born equals: %.0f”, diffSeconds);[/code]


Hi guys!

Here is my solution for the Chapter 14 Challenge. If somebody finds an alternative way to solve the challenge or thinks this one can be improved in any way let us known. Thanks :slight_smile:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        /* instance a NSDateComponents Object 'birthday' initilized with my bithday */
        NSDateComponents *birthday = [NSDateComponents alloc];
        [birthday setYear:1996];
        [birthday setMonth:11];
        [birthday setDay:4];
        [birthday setHour:11];
        [birthday setMinute:43];
        [birthday setSecond:34];
        /* instance a NSCalendar Object 'dayCalendar' provides the configuration of Gregorian Calendar format */
        NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        /* instance a NSDate Object 'dateofBirth'to represent the my birthday for the above Gregorian calendar */
        NSDate *dateOfBirth = [gregorianCalendar dateFromComponents];
        /* instance a NSDate Object 'currentTime' to represent the current time*/
        NSDate *currentTime = [[NSDate alloc] init];
        /* instance NSDate method "timeIntervalSinceDate" returns the seconds between dateOfBirth and currentTime */
        double secondsAlive = [currentTime timeIntervalSinceDate:dateOfBirth];
        NSLog(@"It has been %.0f seconds since I was born.\n", secondsAlive);
    return 0;

++++++++ OUTPUT ++++++++

2015-07-23 19:05:26.132 MoreMessagesChallenge[1199:57061] It has been 63546700028 seconds since I was born.