Obiettivo-C:[nomesectionInfo] di NSFetchedResultsSectionInfo deve essere dateFormatted
-
20-09-2019 - |
Domanda
Ho il codice seguente:
-(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]];
}
Fondamentalmente ho bisogno di convertire la titleforheaderinsection che è una data di stringa come "2009-12-04 00:00:00 +1100" in una stringa più breve dall'aspetto più gradevole.Quindi ho provato a convertirlo utilizzando qualcosa come dateFormatter setDateStyle, ma quando emetto NSLog sulla console ottengo quanto segue:
2009-12-22 09: 42: 10.156 App [94614: 207] Sezione Name 2009-12-04 00:00:00 +1100 2009-12-22 09: 42: 10.157 App [94614: 207] ConvDate (NULL (NULL (NULL
Ovviamente convDate non ottiene nulla, ma [nome sezione Info] dovrebbe essere una stringa.L'ho analizzato nella sua variabile NSString, quindi perché non posso implementare dateFormatter su di essa?
Qualche informazione in più:Analizzo la data tra le altre cose in precedenza, con lo snippet di codice:
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];
Ora, quando eseguo il debug di questo, essenzialmente la stringa grezza è "Sat, 27 Nov 2009 17:16:00 -800" ma quando guardo convDate risulta essere "2009-11-27 00:00:00 + 1100".Non so perché, ma in ogni caso, questo è ciò che viene memorizzato.Avrei pensato che corrispondesse allo stile che ho menzionato, quindi se cambio il formato dateFormatter in qualsiasi altro tipo, si riempirebbe e convDate diventerebbe nil.
Ripensando al mio postController:Ho alcuni frammenti di interesse:
- (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;
}
Spero di ordinare per data e, nel mio codice, in titleForHeaderInSection, formattare la data della stringa in modo che appaia più presentabile.
Grazie ragazzi
Soluzione
Il motivo si stavano diventando un comportamento imprevisto è perché durante l'analisi si stava impostando il formato della data a
[dateFormatter setDateFormat:@"eee, dd MMM yyyy"];
, che non ha informazioni sul tempo, che è il motivo per cui vi era una differenza di tempo nelle date.
Inoltre, durante l'analisi si stava impostando convDate
con
NSDate *convDate = [dateFormatter dateFromString:string];
che è un memorizzando NSDate e cruciale NSFetchResultsController sta chiamando il selettore description
su convDate
perché non è un NSString.
Non sono del tutto sicuro perché NSDateFormatter non poteva riconoscere il formato di data ISO.
Altri suggerimenti
Il motivo per il codice non funziona è perché
[sectionInfo name]
restituisce un oggetto NSString, non un oggetto NSDate, che è richiesta da
[dateFormatter stringFromDate:];
Non si può semplicemente lanciare un NSString a un NSDate.
Invece,
- (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;
}
Inoltre, non autorelease un oggetto a meno che non ci sia un motivo specifico per farlo.
Ho trovato la soluzione andando su http://iosdevelopertips.com/cocoa/date-formatters-examples-take-2.html e utilizzando http://unicode.org/reports/tr35/#Date_Format_Patterns come guida, ho eseguito il debug del mio codice finché non mi sono assicurato che la variabile corrispondesse alla stringa in entrata