ios - ECGraph无法在设备上运行,但可以在模拟器上运行

标签 ios xcode api graph error-handling

有什么理由使ECGraph在模拟器上可以正常运行,但是当我在设备上运行它时却给了我这个错误:

 2012-08-24 01:57:18.543 Portfolio[3538:907] *** -[__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):
 (
0   CoreFoundation                      0x37a1d78f <redacted> + 86
1   Portfolio                           0x000a0d1f -[ECGraph(Private) getDaysFrom:To:] + 202
2   Portfolio                           0x000a1577 -[ECGraph(Private) getXDaysFromLines:minDate:] + 502
3   Portfolio                           0x000a2623 -[ECGraph drawCurveWithLines:lineWidth:color:] + 978
4   Portfolio                           0x000a716f -[GraphView drawRect:] + 2682
5   UIKit                               0x38b1811d <redacted> + 364
6   QuartzCore                          0x39468035 <redacted> + 112
7   QuartzCore                          0x39467771 <redacted> + 1808
8   QuartzCore                          0x39466f45 <redacted> + 980
9   QuartzCore                          0x39466a7b <redacted> + 202
10  QuartzCore                          0x3953a9f5 <redacted> + 24
11  QuartzCore                          0x394663d3 <redacted> + 238
12  QuartzCore                          0x39466119 <redacted> + 316
13  QuartzCore                          0x3945dfe1 <redacted> + 60
14  CoreFoundation                      0x37a4f18d <redacted> + 20
15  CoreFoundation                      0x37a4d3f9 <redacted> + 276
16  CoreFoundation                      0x37a4d74f <redacted> + 742
17  CoreFoundation                      0x379cbc1d CFRunLoopRunSpecific + 356
18  CoreFoundation                      0x379cbaa9 CFRunLoopRunInMode + 104
19  GraphicsServices                    0x3a88f33b GSEventRunModal + 74
20  UIKit                               0x38b35535 UIApplicationMain + 1120
21  Portfolio                           0x00094e7d main + 152
22  Portfolio                           0x00094de0 start + 40

)

最佳答案

您正在尝试从nil NSDate中获取NSDateComponents,如下所示:

NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate* fromDate = nil;
NSDate* toDate = [NSDate date];
unsigned int components = NSYearCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit;
NSDateComponents* dateComponents = [gregorian components:uintFlags fromDate:fromDate  toDate:toDate  options:0];

一个简单的检查可能会有用:
NSDate* fromDate = nil;
NSDate* toDate = [NSDate date];

NSDateComponents* dateComponents = nil;

if(fromDate != nil)
{
 NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
 unsigned int components = NSYearCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit;
 dateComponents = [gregorian components:uintFlags fromDate:fromDate  toDate:toDate  options:0];
}

关于ios - ECGraph无法在设备上运行,但可以在模拟器上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12101989/

相关文章:

ios - 创建主题标签字段以在 Swift 中进行解析

ios - MFMailViewCompose无法关闭

swift - 当键盘存在时未捕获 NSException

ios - LazyVGrid 中的 SwiftUI 中心项目

android - Flutter: InternalLinkedHashMap<String, dynamic >' has no instance method ' cast' with matching arguments

api - Paypal - PayFlow Pro,使用 'Transaction Id' 获取交易细节

javascript - 使用 reactJs 和 chartjs 绘制饼图

ios - Facebook iOS SDK 4.x : changing the user to log in

ios - UITextField 获取当前编辑的单词

ios - 快速将数据从 IOS 应用程序传输到 Watch 应用程序