Chapter 14 solution

[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

    NSDate *date = [NSDate date];
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:1989];
    [comps setMonth:10];
    [comps setDay:30];
    [comps setHour:16];
    [comps setMinute:0];
    [comps setSecond:0];
    
    NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *dateOfBirth = [g dateFromComponents:comps];
    
    
    
    NSLog(@"%f",[date timeIntervalSinceDate:dateOfBirth]);

   
    
}
return 0;

}[/code]

First of all, thank you for your great books. I’m doing my best to learn Objective-C.
I’m using Xcode 5.1 on Maverick OS X 10.9.

For this chapter I finally get to find my way to the solution with this without error :

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {


        NSDate *now = [NSDate date];
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        [comps setYear:1971];
        [comps setMonth:5];
        [comps setDay:03];
        [comps setHour:22];
        [comps setMinute:50];
        [comps setSecond:0];

        NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *dateOfBirth = [g dateFromComponents:comps];

        double secondsSinceEarlierDate = [now timeIntervalSinceDate:dateOfBirth];
        NSLog(@"I've been alive for %.0f seconds.\n", secondsSinceEarlierDate);

    }
    return 0;
}

As I wanted to go further and check my solution I get stuck with a strange error. Can anyone help me please ?

Here is the new code :

#import <Foundation/Foundation.h>

typedef struct {
    double anEntier;
    double mEntier;

} dateHumaineEntiere;

int main(int argc, const char * argv[])
{

    @autoreleasepool {


        NSDate *now = [NSDate date];
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        [comps setYear:1971];
        [comps setMonth:5];
        [comps setDay:03];
        [comps setHour:22];
        [comps setMinute:50];
        [comps setSecond:0];

        NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *dateOfBirth = [g dateFromComponents:comps];

        double secondsSinceEarlierDate = [now timeIntervalSinceDate:dateOfBirth];
        NSLog(@"I've been alive for %.0f seconds.\n", secondsSinceEarlierDate);

        dateHumaineEntiere *maDate;

        double resteAn = modf(secondsSinceEarlierDate/3600/24/365, &maDate->anEntier);
        double nbJoursRestant = 365*resteAn;
        double resteMois = modf(nbJoursRestant/30, &maDate->mEntier);
        resteMois = 0;

        NSLog(@"It represents : \n %f mois, \n %f annees", maDate->mEntier, maDate->anEntier);   <------  Thread 1: EXC_BAD_ACCESS (code =EXC_i386_GPFLT)
        
    }
    return 0;
}

Thank you by advance

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.

Anyway, here was my solution:

[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:1978];
    [comps setMonth:5];
    [comps setDay:6];
    [comps setHour:9];
    [comps setMinute:9];
    [comps setSecond:0];
    
    NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *dateOfBirth = [g dateFromComponents:comps];
    
    double secondsSinceEarlierDate = [[[NSDate alloc] init] timeIntervalSinceDate:dateOfBirth];
    NSLog(@"\nSeconds of life lived: %.0f", secondsSinceEarlierDate);
}
return 0;

}[/code]

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?

NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *dateOfBirth = [g dateFromComponents:comps];

[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?

NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *dateOfBirth = [g dateFromComponents:comps];[/quote]

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’?

here is my solution


#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        NSDate *now = [[NSDate alloc] init];
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        [comps setYear: 1979];
        [comps setMonth: 1];
        [comps setDay: 1];
        [comps setHour: 19];
        [comps setMinute: 31];
        
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
        NSDate *dateOfBirth = [gregorian dateFromComponents: comps];
        double secondsSinceDateOfBirth = [now timeIntervalSinceDate:dateOfBirth];
        
        NSLog(@"I have been alive for %f seconds.", secondsSinceDateOfBirth);
        
    }
    return 0;
}

I did it the way I was asked but also found another way by using the NSTimeInterval class

[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
// Date of birth
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1991];
[comps setMonth:6];
[comps setDay:16];
[comps setHour:24];
[comps setMinute:0];
[comps setSecond:0];
NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *dateOfBirth = [g dateFromComponents:comps];

NSTimeInterval secs = [[NSDate date] timeIntervalSinceDate:dateOfBirth];
NSLog(@"Seconds since birth - %d", (int)secs);

return 0;

}[/code]

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.

File main.m

[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@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

Here is my solution which is typically the same as all of you :stuck_out_tongue:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        
        [comps setYear:1994];
        [comps setMonth:9];
        [comps setDay:10];
        [comps setHour:12];
        [comps setMinute:0];
        [comps setSecond:0];
        
        NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

        NSDate *earlyDate = [g dateFromComponents:comps];
        NSDate *laterDate = [NSDate date];
        
        double secondsSinceEarlierDate = [laterDate timeIntervalSinceDate:earlyDate];
        
        NSLog(@"Date of birth: %@", earlyDate);
        NSLog(@"seconds since 10/9/1994 is %f", secondsSinceEarlierDate);
        
    }
    return 0;
}

But if only someone can make me understand how we should know to figure out how to use things that are similar to this code later

NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

What is going on here is this:

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.

-Robert