2013-08-22 79 views
-3

我從UITableViewCell中的字幕文本設置日期。字幕文本是動態生成的,格式爲「2013年8月21日」。我試圖檢查字幕文本中的日期是否大於當前日期[nsdate日期]的90天,但出現以下錯誤。NSDate stringFromDate返回空錯誤?

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; 
+0

當你的日誌說,你的'detailTextDate'是'NULL',這就是爲什麼你的代碼崩潰。仔細檢查你是如何得到'detailTextDate'' lastEval.date_recorded'。 –

+0

你在那裏有非常好的和有趣的異常處理程序:) –

+0

首先檢查關於「lastEval.date_recorded」?值 – iAppDeveloper

回答

1

檢查TableViewCell。 NSLog(@"Before anything...)已打印出(null)。所以detailTextDate沒有任何價值。

+0

就是這樣。我添加了一條if語句,如下所示:if(lastEval.date_recorded!= nil){}並處理它。謝謝。 –