timeIntervalSince1970 error

The book said two “Create a new Command Line Tool project named TimeAfterTime. Make its type Foundation” The type “Foundation” did not show uo in my version of Xcode (version 7.2), so I choose “Objective C”. It did have the same “#import <Foundation/Foundation.h>” so I assume it was OK to use “Objective C” as the type.

However, when I insert this line of code: “double seconds = [now timeIntervalSince1970];” I get an error that timeIntervalSince1970 is an undeclared identifier. Has timeIntervalSince1970 been dropped or am I doing something wrong?

Thanks for your help…
Earl Staley

You are correct that “Objective-C” was the correct project type, in lieu of “Foundation”.

NSDate’s -timeIntervalSince1970 method is still available. By any chance, might you post your code so that we can see it in context?

Thank you for your reply.

Here is my code:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here…
NSDate *now = [NSDate date];
NSLog(@“This NSDate object lives at address %p”,now);
NSLog(@“The date is %@”,now);
double seconds=[now,timeIntervalSince1970];
}
return 0;
}

The “timeIntervalSince1970” line has a red exclamation point next to it with the message “Use of an undeclared identifier ‘timeIntervalSince1970’”.

BTW, seeing this forum and how it was laid out was a big part of my decision to buy your book. The book is an excellent resource.

Earl Staley

I found the error.

double seconds=[now,timeIntervalSince1970];

should be

double seconds=[now timeIntervalSince1970];

(i.e. no comma)

Thank You…
Earl Staley

What is the meaning of the numbers (18:44:46 +0000) after the date?

The date is 2016-01-02 18:44:46 +0000

Earl Staley

Good eye! Nailed it.

Also, glad you’re liking the book!

An NSDate instance refers not just to a date, but a date and time. Most specifically, a singular moment in time. So the bit after the date is the time (using 24h notation) and the timezone offset of the preceding info; +0000 means GMT.

~Mikey

Hi everyone!! I am stuck again and don’t understand why Xcode 11.5 is writing me an error.
My code is: @autoreleasepool {
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:15];
[comps setMonth:7];
[comps setYear:1975];
[comps setHour:15];
[comps setMinute:0];
[comps setSecond:0];
NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
NSDate *dateOfBirth = [g dateFromComponents: comps];
NSDateComponents *now = [[NSDateComponents alloc] init];
[now setDay:22];
[now setMonth:7];
[now setYear:2005];
[now setHour:15];
[now setMinute:0];
[now setSecond:0];
NSDate *LaterDate = [g dateFromComponents: now];
double seconds = [LaterDate TimeIntervalSinceDate: dateOfBirth]; <<Xcode tell me error at this string "No visible @inreface for ‘NSDate’ declares the selector 'TimeIntervalSinceDate’
NSLog(@“In Your date of birth seconds %f”, seconds);

And next code show me same result:
@autoreleasepool {
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:15];
[comps setMonth:7];
[comps setYear:1975];
[comps setHour:15];
[comps setMinute:0];
[comps setSecond:0];
NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
NSDate *dateOfBirth = [g dateFromComponents: comps];
NSDate *LaterDate = [NSDate date];
NSLog(@"%@", LaterDate);
double seconds = [LaterDate TimeIntervalSinceDate: dateOfBirth];"No visible @inreface for ‘NSDate’ declares the selector 'TimeIntervalSinceDate’ "
NSLog(@“In Your date of birth seconds %f”, seconds);

I think the problem is the upper case T. Try using timeIntervalSinceDate.

https://developer.apple.com/documentation/foundation/nsdate/1410206-timeintervalsincedate?language=objc

Thank you very much!! Now I do not like translators of IT literature into Russian !!