NSDate stringFromDate返回null错误?

时间:2013-08-22 05:07:25

标签: ios objective-c cocoa-touch uitableview

我正在UITableViewCell中设置字幕文本的日期。字幕文本是动态生成的,格式为“2013年8月21日”。我正在尝试检查字幕文本中的日期是否大于当前日期[nsdate date]的90天,但是我收到以下错误。

Before anything: (null)
2013-08-21 23:01:00.656 athletes[806:60b] After NSDate Conversion: (null)
2013-08-21 23:01:00.660 athletes[806:60b] *** -[__NSCFCalendar components:fromDate:toDate:options:]: fromDate cannot be nil
I mean really, what do you think that operation is supposed to mean with a nil fromDate?
An exception has been avoided for now.
A few of these errors are going to be reported with this complaint, then further violations will simply silently do whatever random thing results from the nil.
Here is the backtrace where this occurred this time (some frames may be missing due to compiler optimizations):

这是我尝试过的代码:

NSString *detailTextDate = lastEval.date_recorded;
NSLog(@"Before anything: %@",detailTextDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
NSDate *startDate = [dateFormatter dateFromString:detailTextDate];
NSLog(@"After NSDate Conversion: %@",startDate);
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *todaysDate = [NSDate date];

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
                                                    fromDate:startDate
                                                      toDate:todaysDate
                                                     options:0];
if(components.day >= 90 && lastEval.date_recorded != nil){
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ was last evaluated on :%@. It has been over 3 months since their last evaluation.",athlete.first, lastEval.date_recorded];
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

1 个答案:

答案 0 :(得分:1)

检查您的TableViewCell。 NSLog(@"Before anything...)已打印出(null)。所以detailTextDate没有任何价值。

相关问题