I've been using dateByAddingComponents:toDate:options:
and dateByAddingUnit:value:toDate:options:
and using optional binding to get the date out of it. Like this:
guard let startOfNextMonth = calendar.dateByAddingComponents(oneMonthComponent, toDate: startOfThisMonth, options: []) else {
return nil
}
Where oneMonthComponent
is just an NSDateComponent with the month value set to 1.
When I read about it, in the documentation they both say something like:
Returns nil if date falls outside the defined range of the receiver or if the computation cannot be performed.
And I've been wondering, when is that exactly caused. If I'm just adding a month to a date, is there a way that could ever be nil? Is this because some differences in the other calendrical systems, where adding some units don't make sense?
I have searched around and have been unable to find an instance where it would return nil.
Is there anything that could make the code above be nil? What is an example where adding components could be nil?