So I've just tried what I thought was a simple operation - Adding one day to an NSDate:
Following this answer also on SO which provides the following solution, whilst not exactly what I want (but has the same logic) does not work:
let twoDaysAgo = calendar.dateByAddingUnit(NSCalendarUnit.Day, value: -2, toDate: NSDate(), options: nil)
I get the following error messages:
"Could not find an overload for 'NSDate.init' that accepts the supplied arguments
After removing the NSDate()
argument to form this:
let d = NSDate()
let twoDaysAgo = calendar.dateByAddingUnit(NSCalendarUnit.Day, value: -2, toDate: d, options: nil)
I now get a different error:
'Int' is not convertible to 'IntegerListeralConvertible'
After searching various outlets it turns out that the error is in the final argument options:nil
- which must now be passed a parameter.
Why is this the case?
NSHipster states:
components(_:fromDateComponents:toDateComponents:options:): Returns the difference between two NSDateComponents instances. The method will use base values for any components that are not set, so provide at the least the year for each parameter. The options parameter is unused; pass nil/0.
Whilst Apple do not explicitly state the argument must not be nil
Options for the calculation. See “Calendar Options” for possible values. If you specify a “wrap” option (NSWrapCalendarComponents), the specified components are incremented and wrap around to zero/one on overflow, but do not cause higher units to be incremented. When the wrap option is false, overflow in a unit carries into the higher units, as in typical addition.
Is this an XCode 7 beta bug? New in Swift 2?