+1 to this proposal. The native calendar-based API is bulky and non-intuitive. I find the arguments in previous comments to be an overcomplication of a simple concept, based more on personal feelings than objective reasoning.
For example, to get a date one day later than a given date, we can do:
- Using a calendar or clock instance:
// native way
date = Calendar.current.date(byAdding: .day, value: 1, to: givenDate)
- Using an extension on Date as proposed:
extension Date {
func adding(_ component: Calendar.Component, _ value: Int = 1, calendar: Calendar = .current) -> Date?
}
...
date = givenDate.adding(.day)
- Using a global function:
func add(_ component: Calendar.Component, _ value: Int = 1, to date: Date, calendar: Calendar = .current) -> Date?
...
date = add(.day, to: givenDate) // or even something like `date + 1.days`, but it loses flexibility
The first method is considered correct, while the second is not, because the date itself is not responsible for the human interpretation of time.
In my opinion, all these methods are essentially the same, offering the same functionality with equal flexibility, differing only in syntax. I personally prefer the shortest and most human-like syntax, favoring the second or third method, but definitely not the first. So, +1 to the proposal. I also have a library with frequently implemented Date extensions that may be useful to someone.
PS: You can criticize me, but CLLocationCoordinate2D().street looks absolutely fine to me, very convenient.