Reputation: 7
This application is supposed to Log how many seconds it's been since a birthdate(1996) but when I try and log NSLog(@"It has been %f seconds since the start of 1996.", seconds);
it logs:
2014-01-29 11:36:15.756 TimeAfterTime[750:303] It has been (null) seconds since the start of 1996.
my question is why does it print (null)? This is a Objective-C Command Line Tool application in Xcode. Any suggestions would be greatly appreciated! Thanks
Code I'm using:
// main.m
// TimeAfterTime
//
// Created by JP on 1/28/14.
// Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1996];
[comps setMonth:11];
[comps setDay:7];
[comps setHour:13];
[comps setMinute:23];
[comps setSecond:0];
NSCalendar *g = [[[NSCalendar alloc] init] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *dateOfBirth = [g dateFromComponents:comps];
NSDate *now = [[NSDate alloc] init];
NSLog(@"This NSDate object lives at %p", now);
NSLog(@"The date is %@", now);
double seconds = [now timeIntervalSinceDate:dateOfBirth];
NSLog(@"It has been %f seconds since the start of 1996.", seconds);
}
return 0;
}
The result it prints in the console:
2014-01-29 11:36:15.752 TimeAfterTime[750:303] This NSDate object lives at 0x100103680
2014-01-29 11:36:15.756 TimeAfterTime[750:303] The date is 2014-01-29 16:36:15 +0000
2014-01-29 11:36:15.756 TimeAfterTime[750:303] It has been (null) seconds since the start of 1996.
Program ended with exit code: 0
Upvotes: 0
Views: 50
Reputation: 539775
NSCalendar *g = [[[NSCalendar alloc] init] initWithCalendarIdentifier:NSGregorianCalendar];
should be
NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
Due to the wrong initialization, g = nil
and thus dateOfBirth = nil
.
Upvotes: 3