Skip to content

Instantly share code, notes, and snippets.

@buddax2
Last active October 21, 2019 10:09
Show Gist options
  • Save buddax2/1587103c83ea88163ef5c1e1497d44e5 to your computer and use it in GitHub Desktop.
Save buddax2/1587103c83ea88163ef5c1e1497d44e5 to your computer and use it in GitHub Desktop.
Update or set new date
extension Optional where Wrapped == Date {
mutating func updateOrSet(newDate: Date?, comparedBy: (Date, Date) -> Bool) {
if let currentDate = self, let nDate = newDate {
self = comparedBy(currentDate, nDate) ? currentDate : nDate
}
else {
self = newDate
}
}
}
@buddax2
Copy link
Author

buddax2 commented Oct 21, 2019

Usage:
resultDate.updateOrSet(newDate: lastDate, comparedBy: <)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment