I was trying to practice. .


#1

Can someone tell me what’s wrong with this? I was thinking I could find the number of seconds between any two dates using code from the challenge in this chapter. However, I’m not getting the desired output. Any ideas?

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        [comps setYear: 1979];
        [comps setMonth: 9];
        [comps setDay: 16];
        [comps setHour: 2];
        [comps setMinute: 24];
        [comps setSecond: 0];
        
        NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *ryan = [g dateFromComponents:comps];
        
        
        NSDateComponents *compsB = [[NSDateComponents alloc] init];
        [comps setYear: 1983];
        [comps setMonth: 10];
        [comps setDay: 31];
        [comps setHour: 2];
        [comps setMinute: 24];
        [comps setSecond: 0];
        
        NSCalendar *y = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *adam = [y dateFromComponents:compsB];
        
        double secondsBetweenDates = [adam timeIntervalSinceDate:ryan];
        NSLog(@"There were %f seconds between Ryan and Adam's births.", secondsBetweenDates);
    }
    return 0;
}