ios - 格式化日期时出错

标签 ios crash nsdate nsdatecomponents

我正在使用更改日期格式的代码:

NSDateFormatter *dateFormat=[NSDateFormatter new];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];


    dateFormat.locale=locale;

   [dateFormat setDateFormat:@" dMMyyyy HH:mm:ss"];
   NSDate *date = [dateFormat dateFromString:Currentdate];
  NSDate *today = [dateFormat dateFromString:[datef stringFromDate:[NSDate date]]];
    NSLog(@"date %@ %@",date,Currentdate);

    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSUInteger unitFlags =   NSHourCalendarUnit|NSMinuteCalendarUnit
     ;
    NSDateComponents *components = [gregorian components:unitFlags
                                                fromDate:date
                                                  toDate:today options:0];

但是我得到了错误:

  *** -[__NSCFCalendar components:fromDate:]: date cannot be nil
 I mean really, what do you think that operation is supposed to mean with a nil date?
 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                      0x30ffbad5 <redacted> + 92
1   CoreFoundation                      0x30ffba63 <redacted> + 74
2   VoxSci                              0x000d6997 -[MessagesViewController DayCounting] + 602
3   VoxSci                              0x000d858d -[MessagesViewController tableView:cellForRowAtIndexPath:] + 4000
4   UIKit                               0x33930315 <redacted> + 408
5   UIKit                               0x338d86cd <redacted> + 1800
6   UIKit                               0x338d7ef1 <redacted> + 184
7   UIKit                               0x337fe353 <redacted> + 346
8   QuartzCore                          0x33484943 <redacted> + 142
9   QuartzCore                          0x33480167 <redacted> + 350
10  QuartzCore                          0x3347fff9 <redacted> + 16
11  QuartzCore                          0x3347fa0d <redacted> + 228
12  QuartzCore                          0x3347f81f <redacted> + 314
13  QuartzCore                          0x3347954d <redacted> + 56
14  CoreFoundation                      0x31044f69 <redacted> + 20
15  CoreFoundation                      0x310428f7 <redacted> + 286
16  CoreFoundation                      0x31042c43 <redacted> + 738
17  CoreFoundation                      0x30fad471 CFRunLoopRunSpecific + 524
18  CoreFoundation                      0x30fad253 CFRunLoopRunInMode + 106
19  GraphicsServices                    0x35ce72eb GSEventRunModal + 138
20  UIKit                               0x33862845 UIApplicationMain + 1136

22  libdyld.dylib                       0x3b8cfab7 <redacted> + 2

)

我已经搜索过它但得到了类似的东西。有人可以告诉我问题所在或我错在哪里吗? 任何帮助将不胜感激。提前致谢。

最佳答案

我觉得你打错了:) 尝试

[dateFormat setDateFormat:@"ddMMyyyy HH:mm:ss"];

关于ios - 格式化日期时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21299609/

相关文章:

iphone - 控制台错误 : failed to find PDF header: `%PDF' not found

android-studio - 退格键导致计算器崩溃

iphone - 在 UIImageView 上删除

c++ - 普通比较时QT 4.8崩溃

visual-studio-2010 - VS2010启动崩溃,在安全模式下运行,所有加载项均已删除,仍然崩溃

swift - Swift 中是否只有日期(没有时间)类?

ios - 从两个 NSDates 中获取本地化的日期范围字符串

iphone - NSDate 表示该月的第一天

ios - 在 nsoperation 内部使用 Runloop 执行异步任务

ios - 数据始终为空……不知道为什么