I want to get a quarter value in NSDateComponents class

It seems that there is an issue with the NSQuarterCalendarUnit being ignored (haven’t verified, but did see a bug reported). Anyway, it is simple enough to get around… Just use the NSDateFormatter class. // After your NSDate *date, add… NSDateFormatter *quarterOnly = [[NSDateFormatter alloc]init]; [quarterOnly setDateFormat:@”Q”]; int quarter = [[quarterOnly stringFromDate:date] intValue]; // Then change … Read more

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

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

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