This function gets the date from yesterday; it is working on my emulator but it crashes with my real device.
I am working with Xcode6 Beta 6.
var tuple :(value Int, unit:NSCalendarUnit) = (1, NSCalendarUnit.CalendarUnitDay)
var date = NSDate()
var yesterday = NSCalendar.currentCalender().dateByAddingUnit(tuple.unit, value: (-tuple.value), toDate: date, options: NSCalendarOptions.SearchBackwards)
This is the error I get on my real device:
-[_NSCopyOnWriteCalendarWrapper dateByAddingUnit:value:toDate:options:]: unrecognized selector sent to instance 0x176cca10
When I ran your code it has syntax errors to begin with. I just took the following code and ran it both on a device in playground, in a online simulator and an xcode project and it works for all three.
The following works for me.
http://swiftstub.com/992344166/ Click Run to see result.
Please try below code
I recently had a similar error.
I suspect you're trying to run the code on a pre 8.0 device. If that's the case,
dateByAddingUnit
is not available on your device (see the header)You can achieve what you're trying to do by using `dateByAddingComponents' - it's a little more cumbersome but should give you the same result. Try the following playground
Hope this helps! :)