我正在尝试组合字符串日期和时间,然后将其转换为 NSDate。我的代码是:
NSMutableArray *arrayOfDatesAsDates = [[NSMutableArray alloc] init];
NSDateFormatter *dateAndTimeFormatter = [[NSDateFormatter alloc] init];
[dateAndTimeFormatter setLocale:enUSPOSIXLocale];
[dateAndTimeFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];
NSLog(@"here");
//create an NSDate with todays date and the right prayer time
NSString *prayerDateString = [curDate stringByAppendingString: @" "];
prayerDateString = [prayerDateString stringByAppendingString: timeOfMagrib];
NSDate *prayerDateAndTime = [dateAndTimeFormatter dateFromString:prayerDateString]; //convert string back to date
NSLog(@"nsdate %@", prayerDateAndTime);
[arrayOfDatesAsDates addObject:prayerDateAndTime];
如预期的那样,prayDateAndTime 日志的输出为 2013-07-08 20:26:00 +0000 ,错误消息为Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
.
它在线路上崩溃[arrayOfDatesAsDates addObject:prayerDateAndTime];
。
为什么是这样?
非常感谢