Swift 2.0 calendar components error

As of Swift 2, NS_OPTIONS (such as NSCalendarOptions) are mapped to Swift as a OptionSetType which offers a set-like interface. In particular, “no options” can now be specified as [] instead of nil: let components = cal.components(unit, fromDate: calcDesp!, toDate: calHoy!, options: []) See also Swift 2.0 – Binary Operator “|” cannot be applied to … Read more

Get day of week using NSDate

Swift 3 & 4 Retrieving the day of the week’s number is dramatically simplified in Swift 3 because DateComponents is no longer optional. Here it is as an extension: extension Date { func dayNumberOfWeek() -> Int? { return Calendar.current.dateComponents([.weekday], from: self).weekday } } // returns an integer from 1 – 7, with 1 being Sunday … Read more

UILocalNotification Repeat Interval for Custom Alarm (sun, mon, tue, wed, thu, fri, sat)

You cannot set custom repeat intervals with UILocalNotification. This has been asked before (see below) but only limited options are provided. The repeatInterval parameter is an enum type and it limited to specific values. You cannot multiply those enumerations and get multiples of those intervals. You cannot have more than 64 local notifications set in … Read more

Differences in NSDateComponents syntax?

Swift 2 The NSCalendarUnit names have changed in Swift 2. Also, now we have to pass these arguments in an OptionSet, like this: let components = calendar.components([.Hour, .Minute, .Second, .Nanosecond], fromDate: date) Swift 3 Many things have changed, according to the Swift API Design Guidelines. Updated syntax: let date = Date() let calendar = Calendar.current() … Read more

NSCalendar first day of week

Edit: This does not check the edge case where the beginning of the week starts in the prior month. Some updated code to cover this: https://stackoverflow.com/a/14688780/308315 In case anyone is still paying attention to this, you need to use ordinalityOfUnit:inUnit:forDate: and set firstWeekday to 2. (1 == Sunday and 7 == Saturday) Here’s the code: … Read more