Running|Code

About software development and trail running.

Undocumented NSCalendar Methods

While working on an iOS app, I found myself writing some NSCalendar extension methods such as beginningOfDay(relativeToDate:), thinking that the NSCalendar class was a little lacking. Then I stumbled upon some undocumented NSCalendar methods that accomplish exactly what I was looking for.

These methods are not yet mentioned in the latest iOS and OS X documentation, though they were added to the frameworks as of iOS 8.0 and OS X 10.9. I found them when I command-clicked an another NSCalendar method to check on its signature, and noticed a lot of new convenience methods for constructing and enumerating dates. Check the iOS 8 API diffs to see everything that was added. Hopefully this will save someone some effort.

Before finding these new APIs, I had finished writing my own version of startOfDayForDate using TDD. I removed my own extension methods and updated the app to use the builtin methods, but I kept the unit tests that were already written since they function as some nice documentation. I published these unit tests as a Gist in case they prove useful to others.

Update: NSHipster published an in-depth article about all of the recent NSCalendar API additions.

This post was modified after its initial publication.