-3

我正在从 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;
4

1 回答 1

1

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

于 2013-08-22T05:13:16.893 回答