Skip to main content

Posts

Showing posts with the label Extensions

Getting the dayBefore and dayAfter from NSDate (in Swift)

So before I write part 2 of the story of how my first mobile( iOS ) app came to be, this would be a small post on how to use Extensions in Swift, to make code more readable...amongst other things. Problem So in my iOS app, there are a few places where I have to get the day before(yesterday) and the day after(tomorrow) for a date.  Solution In the first version of my code, I had the code such as this (Read on to find out a more correct solution for calculating dates) For the day after let oneDay:Double = 60 * 60 * 24 return self.dateByAddingTimeInterval(oneDay) For the day before let oneDay:Double = 60 * 60 * 24 return self.dateByAddingTimeInterval(-(Double(oneDay))) one day has 24 hours, each hour has 60 mins and each minute has 60 seconds, so 60 * 60  * 24 to calculate the solution to my problem. This was working and it was all fine...until I delved deeper into Swift(by reading this  ebook  by Apple) and found out about Extensions!!! It ...