我正在我的应用程序中解析RSS提要并将每个项目放入表格视图单元格中。我抓住的其中一件是pubDate
,这是一个如下所示的字符串:
Fri, 23 May 2014 10:00:00 GMT
我使用以下代码,但NSDate
继续返回null。但是,如果我手动输入字符串(请参阅注释掉的行)而不是尝试抓取pubDate
,它可以正常工作。知道我做错了吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ReadingsCell *cell = (ReadingsCell *)[tableView dequeueReusableCellWithIdentifier:@"ReadingsCell"];
cell.readingLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
dateString = [[feeds objectAtIndex:indexPath.row] objectForKey: @"pubDate"];
//dateString = @"Mon, 19 May 2014 10:00:00 GMT";
NSLog(@"dateString: %@", dateString);
//the above logs out perfectly
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSDate *postDate = [[NSDate alloc] init];
postDate = [dateFormat dateFromString:dateString];
NSLog(@"pubdate: %@", postDate);
//this logs out as null
[dateFormat setDateFormat:@"MMM"];
NSString *theMonth = [dateFormat stringFromDate:postDate];
cell.monthLabel.text = theMonth;
return cell;
}