NSFetchedResultsSectionInfo的Objective-C:[sectionInfo名称]需要进行日期格式化
我有以下代码:NSFetchedResultsSectionInfo的Objective-C:[sectionInfo名称]需要进行日期格式化,objective-c,iphone,iphone-sdk-3.0,nsfetchedresultscontroller,Objective C,Iphone,Iphone Sdk 3.0,Nsfetchedresultscontroller,我有以下代码: -(NSString *)tableView:(UITableView *)table titleForHeaderInSection:(NSInteger)section { id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; NSDateFormatter* dateFormatter = [[
-(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]];
}
现在,当我调试这个时,基本上原始字符串是“Sat,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;
}
我希望按日期排序,在我的代码中,在titleforheaderin部分,格式化我的字符串日期,使其看起来更形象
谢谢大家您的代码不起作用的原因是
[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;
}
-(NSString*)表格视图:(UITableView*)表格标题标题标题部分:(NSInteger)部分
{
id sectionInfo=[[fetchedResultsController节]对象索引:节];
NSString*sectionName=[sectionInfo name];
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateStyle:nsDateFormatTermeNiumStyle];
NSString*convDate=[dateFormatter stringFromDate:[dateFormatter dateFromString:sectionName]];
[日期格式化程序发布];
返回日期;
}
另外,除非有特定的原因,否则不要自动释放对象。我通过转到并使用作为指南找到了解决方案,我调试了我的代码,直到我确保变量与传入字符串匹配为止。之所以出现意外行为,是因为在解析过程中,您将日期格式设置为
[dateFormatter setDateFormat:@"eee, dd MMM yyyy"];
它没有时间信息,这就是为什么日期有时差
此外,在解析过程中,您使用
NSDate *convDate = [dateFormatter dateFromString:string];
它正在存储NSDate,而关键的是,NSFetchResultsController正在调用convDate
上的说明
选择器,因为它不是NSString
我不完全清楚为什么NSDateFormatter无法识别ISO日期格式。这意味着
dateFromString
没有返回日期,或者stringfromsdate
没有返回字符串。您可以跟踪正在发生的错误并跟踪错误的来源。我猜问题是[sectionInfo name]返回的值。我意识到这是因为日期样式错误。您需要将日期样式设置为long并调用dateFromString,然后将日期样式设置为medium并调用stringFromDate。。[dateFormatter setDateStyle:NSDateFormatterLongStyle]。。。NSDate*convStringDate=[dateFormatter dateFromString:[sectionInfo name]];[dateFormatter setDateStyle:nsDateFormatTermeLiumStyle];。。。NSString*convDateString=[dateFormatter stringFromDate:convStringDate]-我仍然没有得到任何回报。我肯定知道[sectioninfo name]正在返回文本。我还将convStringDate记录为long date,但它仍然返回null。在这种情况下,NSDateFormatter不会将[sectionInfo name]值识别为格式正确的日期字符串。要回答这个问题,我需要看到更多的代码。我觉得这很奇怪,因为NSString接受在sectionName中为其分配[sectionInfo name]。我认为解决方案的一个线索是,[sectionInfo name]的原始值是2009-12-04 00:00:00+1100。
NSDate *convDate = [dateFormatter dateFromString:string];