Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Blog Reader iPhone App Adapting Data for Display Formatting Dates with NSDateFormatter

Receiving Error from NSDictionary reference after NSDate Implementation

Hello all,

I have followed the "BlogReader" project taught by Amit (Hi Amit!) Baljani since the first video. In a previous video, we were taught how to reference JSON data from the following URL

http://blog.teamtreehouse.com/api/get_recent_summary/

Using the following code:

NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;

In other words, a NSURL variable is set with the JSON data from the URL using URLWithSTring. The JSON URL is stored in an NSData variable using dataWithContentsOfURL. I'm not sure how it works, but there is an error variable that is assigned to nil using NSError.

This all works with no problems. The table view is viewed as follows:

cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", blogPost.author, blogPost.date];

However, after adding the following "date formatting" module to my BlogPost.h header file:

- (NSString *) formattedDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *tempDate = [dateFormatter dateFromString:self.date];

    [dateFormatter setDateFormat:@"EE MMM, dd"];
    return [dateFormatter stringFromDate:tempDate];
}

and then adding that formatted date variable to my table view:

cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", blogPost.author, [blogPost formattedDate]];

I receive an error, which takes me to the following lines after inserting an exception breakpoint:

NSError *error = nil;

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

Any ideas?

thanks

2 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

It's something weird with your "formattedDate" method, although I don't see what, but I'm pretty tired right now :P

Try adding this instead of your "formattedDate" and see if you get the same error:

- (NSString *)todaysDate
{
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyMMdd"];

    return [formatter stringFromDate:date];

}

Hmm! I'm not sure why, but that did the trick!

As they say... "It's 11pm... your app's not working... and you have no idea why... It's now 3am, your app is finally working, and you have no idea why..."