Objective-C:NSFetchedResultsSectionInfo的[sectionInfo name]需要是dateFormatted

时间:2009-12-21 22:46:30

标签: objective-c iphone iphone-sdk-3.0 nsfetchedresultscontroller

我有以下代码:

   -(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]];
 }

我基本上需要将标题为“2009-12-04 00:00:00 +1100”的字符串日期转换为更好看的短字符串。所以我尝试使用dateFormatter setDateStyle之类的东西来转换它,但是当我将NSLog输出到控制台时,我得到以下内容:

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

显然,convDate没有得到任何东西,但[sectionInfo name]应该是一个字符串。我已将其解析为自己的NSString变量,为什么我不能在其上实现dateFormatter?


更多信息:我在之前的其他内容中解析日期,代码片段为:

   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];

现在,当我调试这个时,基本上原始字符串是“星期六,2009年11月27日17:16:00 -800”但是当我看到convDate时它出现了“2009-11-27 00:00 :00 +1100“。不知道为什么,但无论如何,这就是存储的东西。我原以为它会与我提到的样式相匹配,所以如果我将dateFormatter格式更改为任何其他类型,它会填满并且convDate变为零。

回顾我的postController:我有一些感兴趣的片段:

- (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;

}

我希望按日期排序,然后在我的代码中,在titleForHeaderInSection中,将我的字符串日期格式化为更具代表性。

谢谢你们

3 个答案:

答案 0 :(得分:1)

您的代码无效的原因是

[sectionInfo name]

返回NSString对象,而不是

所需的NSDate对象
[dateFormatter stringFromDate:];

您不能简单地将NSString强制转换为NSDate。

相反,

- (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;
}

此外,除非有特殊原因,否则不要自动释放对象。

答案 1 :(得分:1)

您遇到意外行为的原因是因为在解析过程中您将日期格式设置为

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

没有时间信息,这就是为什么日期有时差。

此外,在解析过程中,您使用

设置convDate
NSDate *convDate = [dateFormatter dateFromString:string];

存储NSDate且至关重要 NSFetchResultsController正在调用description上的convDate选择器,因为它不是NSString。

我不完全确定为什么NSDateFormatter无法识别ISO日期格式。

答案 2 :(得分:0)

我通过转到http://iosdevelopertips.com/cocoa/date-formatters-examples-take-2.html并使用http://unicode.org/reports/tr35/#Date_Format_Patterns作为指南找到了解决方案,我调试了代码,直到确保变量与传入字符串匹配