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
Richard Lu
20,185 PointsNSDate dateFromString method returning nil
for(NSDictionary *dictionary in postTitles) {
[title addObject:[dictionary objectForKey:@"title"]];
[date addObject:[dictionary objectForKey:@"date"]];
NSDateFormatter *actualDateFormat = [[NSDateFormatter alloc] init];
[actualDateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *dateObject = [actualDateFormat dateFromString: [date objectAtIndex:[date count]-1]];
NSLog(@"%d:%@", counter, dateObject);
counter++;
}
Would anyone have any clue to why this is returning nil? I'm using the teamtreehouse JSON Api to gather a date. From the documentation it says that it only returns nil if DateFormatter can't parse it into an NSDate Object, but why is this happening. How would I fix this?
Here's what I'm getting
FunWithDate[3097:60b] 0:(null)
FunWithDate[3097:60b] 1:(null)
FunWithDate[3097:60b] 2:(null)
FunWithDate[3097:60b] 3:(null)
FunWithDate[3097:60b] 4:2014-04-29 15:53:40 +0000
FunWithDate[3097:60b] 5:2014-04-28 04:28:56 +0000
FunWithDate[3097:60b] 6:(null)
FunWithDate[3097:60b] 7:2014-04-23 15:14:51 +0000
FunWithDate[3097:60b] 8:(null)
FunWithDate[3097:60b] 9:2014-04-22 13:56:06 +0000
3 Answers
Richard Lu
20,185 PointsHey Dennis,
Thanks for answering! I've tried that and it still doesn't work. The documentation mentions this:
dateFromString "A date representation of string interpreted using the receiver’s current settings. If dateFromString: can not parse the string, returns nil."
The blogPosts all have a date, it's just that the formatter is unable to parse the retrieve data. :(
Dennis Walsh
5,793 PointsRichard,
Have you tried moving the date formatter outside of the loop? I would think you could reuse the date formatter and date obejct for each blog post date rather then creating a new date formatter and date objecct for each date. Have you confirmed that the blog post does have a date?
NSDateFormatter *actualDateFormat = [[NSDateFormatter alloc] init];
[actualDateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *dateObject = nil;
for(NSDictionary *dictionary in postTitles)
{
[title addObject:[dictionary objectForKey:@"title"]];
[date addObject:[dictionary objectForKey:@"date"]];
dateObject = [actualDateFormat dateFromString: [date objectAtIndex:[date count]-1]];
NSLog(@"%d:%@", counter, dateObject);
counter++;
dateObject = nil; //clear it just to be safe
}
Dennis Walsh
5,793 PointsRichard,
Have you tried putting a breakpoint at the beginnig of the loop? You can then check the value of each variable by stepping through the code and seeing in the debugger the exact value of the date for each post. This might give you a hint as to why some posts work while other posts do not. It is a little hard for me to check your code since it is only a partial post of the code. Is you code posted anywhere like github?