Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.

Fix compatibility issue NSDate add #101

Merged
merged 3 commits into from
Mar 31, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion ExSwift/NSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,32 @@ public extension NSDate {
*/
public func add(seconds: Int = 0, minutes: Int = 0, hours: Int = 0, days: Int = 0, weeks: Int = 0, months: Int = 0, years: Int = 0) -> NSDate {
var calendar = NSCalendar.currentCalendar()
var date = calendar.dateByAddingUnit(.CalendarUnitSecond, value: seconds, toDate: self, options: nil) as NSDate!
let version = floor(NSFoundationVersionNumber)
if ( version <= NSFoundationVersionNumber_iOS_7_1 || version <= NSFoundationVersionNumber10_9_2 ) {
var component = NSDateComponents()
component.setValue(seconds, forComponent: .CalendarUnitSecond)
var date : NSDate! = calendar.dateByAddingComponents(component, toDate: self, options: nil)!
component = NSDateComponents()
component.setValue(minutes, forComponent: .CalendarUnitMinute)
date = calendar.dateByAddingComponents(component, toDate: date, options: nil)!
component = NSDateComponents()
component.setValue(days, forComponent: .CalendarUnitDay)
date = calendar.dateByAddingComponents(component, toDate: date, options: nil)!
component = NSDateComponents()
component.setValue(hours, forComponent: .CalendarUnitHour)
date = calendar.dateByAddingComponents(component, toDate: date, options: nil)!
component.setValue(weeks, forComponent: .CalendarUnitWeekOfMonth)
date = calendar.dateByAddingComponents(component, toDate: date, options: nil)!
component = NSDateComponents()
component.setValue(months, forComponent: .CalendarUnitMonth)
date = calendar.dateByAddingComponents(component, toDate: date, options: nil)!
component = NSDateComponents()
component.setValue(years, forComponent: .CalendarUnitYear)
date = calendar.dateByAddingComponents(component, toDate: date, options: nil)!
return date
}

var date : NSDate! = calendar.dateByAddingUnit(.CalendarUnitSecond, value: seconds, toDate: self, options: nil)
date = calendar.dateByAddingUnit(.CalendarUnitMinute, value: minutes, toDate: date, options: nil)
date = calendar.dateByAddingUnit(.CalendarUnitDay, value: days, toDate: date, options: nil)
date = calendar.dateByAddingUnit(.CalendarUnitHour, value: hours, toDate: date, options: nil)
Expand Down