开发者

Objective-C: [sectionInfo name] of NSFetchedResultsSectionInfo needs to be dateFormatted

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:[[NSSortDesc开发者_StackOverflow社区riptor 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


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜