NSCFCalendar dateFromComponents:组件不能为零

NSCFCalendar dateFromComponents: components cannot be nil

执行以下代码后

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *expiryDateComponents = [calendar components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:aDate];

您收到以下非崩溃异常

*** -[__NSCFCalendar dateFromComponents:]: components cannot be nil
Future exception.
A few of these errors are going to be reported with this complaint, then further violations will simply be ignored.

tl;dr 确保 fromDate 参数是 not nil.

解释:

NSCalendar 不知道如何处理当你给它一个 nil NSDate 的情况。 API 是给你一个警告,而不是抛出崩溃异常。请注意,由于 API 更改,此行为可能会更改,并且将来可能会导致崩溃。