Chapter 15 Command line tool dateList


#1

I am working this out and am getting some confusion. I have two problems.

1st: The dates are for some reason wrong. My Output is

2013-07-11 22:43:27.912 DateList[2761:303] There are 3 dates
2013-07-11 22:43:27.918 DateList[2761:303] The first date is 2013-07-13 02:43:27 +0000
2013-07-11 22:43:27.918 DateList[2761:303] The second date is 2013-07-11 02:43:27 +0000
2013-07-11 22:43:27.919 DateList[2761:303] *** Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 … 2]’
*** First throw call stack:
(
0 CoreFoundation 0x00007fff9718db06 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8f9eb3f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff97140b53 -[__NSArrayI objectAtIndex:] + 163
3 DateList 0x0000000100000dc5 main + 469
4 libdyld.dylib 0x00007fff97fc57e1 start + 0
)
libc++abi.dylib: terminate called throwing an exception
(lldb)

Why the whole lldb thing and all this extra stuff? It goes away when their are only 2 dates quoted.

and as you can see the dates are wrong if you look at the date it says, and the date it says i ran the program at.

Im only 13, so im trying to learn all this, and this may be a simple mistake i made. thanks in advance for any help.

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        // insert code here...
        NSDate *now = [NSDate date];
        NSDate *tomorrow = [now dateByAddingTimeInterval:24.0 * 60.0 * 60.0];
        NSDate *yesterday = [now dateByAddingTimeInterval:-24.0 * 60.0 * 60.0];
        
        NSArray *dateList = [NSArray arrayWithObjects:now, tomorrow, yesterday, nil];
        
        NSLog(@"There are %lu dates", [dateList count]);
        
        NSLog(@"The first date is %@", [dateList objectAtIndex:1]);
        NSLog(@"The second date is %@", [dateList objectAtIndex:2]);
        NSLog(@"the third date is %@", [dateList objectAtIndex:3]);
        
    }
    return 0;
}

#2

Perfect age to start learning to program, just like learning to play a musical instrument.

[quote]Why the whole lldb thing and all this extra stuff? It goes away when their are only 2 dates quoted.
[/quote]
That means your program has crashed, due to a bug.

The reason your program has crashed is because you are trying to get an item that does not exist in the array.
You have three items in the array but you are trying to get a fourth item with [dateList objectAtIndex:3], which means give me the fourth item (because the index is 3.)

Array indexes are zero based; meaning they start at zero and end at N -1, where N is the number of items in the array.

As for the dates, print them with a date formatter:

Here is the revised code:

// main.m

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        NSDate *now = [NSDate date];
        NSDate *tomorrow  = [now dateByAddingTimeInterval:24.0 * 60.0 * 60.0];
        NSDate *yesterday = [now dateByAddingTimeInterval:-24.0 * 60.0 * 60.0];
        
        NSArray *dateList = [NSArray arrayWithObjects:now, tomorrow, yesterday, nil];
        
        NSLog (@"There are %lu dates", [dateList count]);
        
        // Use valid indexes to get the items from the array
        NSLog (@"The first date is  %@", [dateList objectAtIndex:0]);
        NSLog (@"The second date is %@", [dateList objectAtIndex:1]);
        NSLog (@"the third date is  %@", [dateList objectAtIndex:2]);
        
        // Print the dates with a date formatter
        // so that they are printed for the current locale not for UTC
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeStyle:NSDateFormatterLongStyle];
        [dateFormatter setDateStyle:NSDateFormatterLongStyle];
        
        NSString *firstDate = [dateFormatter stringFromDate:[dateList objectAtIndex:0]];
        NSLog (@"The first date is  %@",  firstDate);
        
        NSString *secondDate = [dateFormatter stringFromDate:[dateList objectAtIndex:1]];
        NSLog (@"The second date is %@", secondDate);
        
        NSString *thirdDate = [dateFormatter stringFromDate:[dateList objectAtIndex:2]];
        NSLog (@"the third date is  %@",  thirdDate);
    }
    return 0;
}

#3

Ohh. I see now, so the object at indexes start at 0 not 1 and I need to print the dates in the locale. Ok thank you very much for your help.