Skip to content

Commit

Permalink
Fixed:
Browse files Browse the repository at this point in the history
  - Error in add() where nil values were not being handled correctly with valid defaults
  - error in set() where components.minute could be set to seconds instead of minutes

Cleaned up code in set() to use the nil coalescing operator (??) for brevity
  • Loading branch information
toriaezunama committed Jun 6, 2015
1 parent c488889 commit 7928e76
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions SwiftDate/SwiftDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,12 @@ public extension NSDate {
*/
func set(#year: Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, tz: String?) -> NSDate! {
let components = self.components
components.year = (year != nil ? year! : self.year)
components.month = (month != nil ? month! : self.month)
components.day = (day != nil ? day! : self.day)
components.hour = (hour != nil ? hour! : self.hour)
components.minute = (minute != nil ? minute! : self.second)
components.second = (second != nil ? second! : self.second)
components.year = year ?? self.year
components.month = month ?? self.month
components.day = day ?? self.day
components.hour = hour ?? self.hour
components.minute = minute ?? self.minute
components.second = second ?? self.second
components.timeZone = (tz != nil ? NSTimeZone(abbreviation: tz!) : NSTimeZone.defaultTimeZone())
return NSCalendar.currentCalendar().dateFromComponents(components)
}
Expand Down Expand Up @@ -394,13 +394,13 @@ public extension NSDate {
*/
func add(#years: Int?, months: Int?, weeks: Int?, days: Int?,hours: Int?,minutes: Int?,seconds: Int?) -> NSDate {
var components = NSDateComponents()
components.year = years ?? years!
components.month = months ?? months!
components.weekOfYear = weeks ?? weeks!
components.day = days ?? days!
components.hour = hours ?? hours!
components.minute = minutes ?? minutes!
components.second = seconds ?? seconds!
components.year = years ?? 0
components.month = months ?? 0
components.weekOfYear = weeks ?? 0
components.day = days ?? 0
components.hour = hours ?? 0
components.minute = minutes ?? 0
components.second = seconds ?? 0
return self.addComponents(components)
}

Expand Down

0 comments on commit 7928e76

Please sign in to comment.