0

我有以下代码:

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

我基本上需要将 titleforheaderinsection 这是一个字符串日期,如“2009-12-04 00:00:00 +1100”转换为一个更好看的更短的字符串。因此,我尝试使用 dateFormatter setDateStyle 之类的方法对其进行转换,但是当我将 NSLog 输出到控制台时,我得到以下信息:

2009-12-22 09:42:10.156 app[94614:207] 部分名称 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];

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

回顾我的 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 中,格式化我的字符串日期以看起来更美观。

多谢你们

4

3 回答 3

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

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

于 2009-12-21T23:17:32.510 回答
1

您出现意外行为的原因是因为在解析期间您将日期格式设置为

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

它没有时间信息,这就是日期存在时差的原因。

此外,在解析过程中,您正在convDate设置

NSDate *convDate = [dateFormatter dateFromString:string];

它存储了一个 NSDate,关键是NSFetchResultsController 正在调用description选择器,convDate因为它不是一个 NSString。

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

于 2009-12-22T14:47:15.073 回答
0

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

于 2009-12-22T04:21:25.767 回答