views:

347

answers:

3

Hi guys,

I have the following code:

   -(NSString *)tableView:(UITableView *)table titleForHeaderInSection:(NSInteger)section { 
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];


NSString *sectionName = [sectionInfo name];
NSLog(@"sectionName %@", sectionName);


NSString *convDate = [dateFormatter stringFromDate: (NSDate *)sectionName];
NSLog(@"convDate %@", convDate);
return [NSString stringWithFormat:@"%@", [sectionInfo name]];
 }

I am basically needing to convert the titleforheaderinsection which is a string date like "2009-12-04 00:00:00 +1100" to a nicer looking shorter string. So I have tried converting it using something like dateFormatter setDateStyle, but when i output the NSLog to console i get the following:

2009-12-22 09:42:10.156 app[94614:207] sectionName 2009-12-04 00:00:00 +1100 2009-12-22 09:42:10.157 app[94614:207] convDate (null

Obviously the convDate is not getting anything, but [sectionInfo name] should be a string. I have parsed it into its own NSString variable, so why cant i implement the dateFormatter on it?


A bit more information: I parse the date amongst other things earlier on, with the code snippet being:

   if ([currentElement isEqualToString: @"date"]) { 
 NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init] ;
 [dateFormatter setDateFormat:@"eee, dd MMM yyyy"];
 NSDate *convDate = [dateFormatter dateFromString:string];
 if (self.isComment){
  [currentComment setDate: convDate];   
 }else if (self.isPost) 
  NSLog(@"convDate is %@", convDate);
  [currentPost setDate: convDate];

Now, when I debug this, essentially the raw string is "Sat, 27 Nov 2009 17:16:00 -800" but when i look at the convDate it comes out to be "2009-11-27 00:00:00 +1100". Not sure why, but in any case, thats what gets stored. I would have thought it would match the style i mentioned, so if i change the dateFormatter format to any other type, it would stuff up and convDate become nil.

Looking back at my postController: I have some snippets of interest:

- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController == nil) {
    NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Post" inManagedObjectContext: ApplicationController.sharedInstance.managedObjectContext]];
    NSArray *sortDescriptors = nil;
    NSString *sectionNameKeyPath = @"date";



 NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(PostSite.name like '%@')", self.site.name]];

 [fetchRequest setPredicate:pred];

 sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] 
             initWithKey:@"date" ascending:NO] ];
    [fetchRequest setSortDescriptors:sortDescriptors];
    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext: 
        ApplicationController.sharedInstance.managedObjectContext 
                  sectionNameKeyPath:sectionNameKeyPath cacheName:@"PostCache"];
}    

return fetchedResultsController;

}

I am hoping to sort by date, and up in my code, in titleForHeaderInSection, format my string date to look more presentable.

Thanks guys

+1  A: 

The reason your code does not work is because

[sectionInfo name]

returns an NSString object, not an NSDate object, which is required by

[dateFormatter stringFromDate:];

You cannot simply cast an NSString to an NSDate.

Instead,

- (NSString *)tableView:(UITableView *)table titleForHeaderInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    NSString *sectionName = [sectionInfo name];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

    NSString *convDate = [dateFormatter stringFromDate:[dateFormatter dateFromString:sectionName]];
    [dateFormatter release];

    return convDate;
}

Also, don't autorelease an object unless there is a specific reason to do so.

Oliver White
It means that either `dateFromString` is not returning a date or `stringFromDate` is not returning a string. You can track down which is happening and follow the source of the error. I would guess that the problem is the value returned by [sectionInfo name].
Oliver White
I've realized that it is because the date style is wrong. You need to set the date style to long and call dateFromString, then set the date style to medium and call stringFromDate.
Oliver White
OK Oliver, ive done this: .. [dateFormatter setDateStyle:NSDateFormatterLongStyle]; ... NSDate *convStringDate = [dateFormatter dateFromString:[sectionInfo name]]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle];... NSString *convDateString = [dateFormatter stringFromDate:convStringDate];-im still not getting anything back. I know for certain [sectioninfo name] is returning text. I also NSLogged convStringDate for long date and still it returns null
Doron Katz
In that case, NSDateFormatter does not recognise the [sectionInfo name] value as a properly formatted date string. To answer this, I'd need to see a lot more code.
Oliver White
Well i find it quite bizzare because NSString accepts the assignment of [sectionInfo name] to it in sectionName. What i think might be a clue to the solution is that the raw value of [sectionInfo name] is 2009-12-04 00:00:00 +1100.
Doron Katz
A: 

I found the solution by going to http://iphonedevelopertips.com/cocoa/date-formatters-examples-take-2.html and using http://unicode.org/reports/tr35/#Date%5FFormat%5FPatterns as a guide, I debugged my code until i ensured that the variable is matched with the incoming string

Doron Katz
+1  A: 

The reason you were getting unexpected behaviour is because during parsing you were setting the date format to

[dateFormatter setDateFormat:@"eee, dd MMM yyyy"];

which has no time information, which is why there was a time difference in the dates.

Also, during parsing you were setting convDate with

NSDate *convDate = [dateFormatter dateFromString:string];

which is storing an NSDate and crucially NSFetchResultsController is calling the description selector on convDate because it is not an NSString.

I'm not entirely sure why NSDateFormatter could not recognise the ISO date format.

Oliver White