1

enter image description here

I don't know what is wrong with the below code. I think it should work. The error I got is EXC_BAD_ACCESS

// Playground - noun: a place where people can play

import UIKit

extension NSCalendar {
    func nextDay(date:NSDate) -> NSDate {
        var dateComponents = self.components(.YearCalendarUnit | .MonthCalendarUnit | .DayCalendarUnit, fromDate: date)
        dateComponents.day += 1
        let nextDate = self.dateFromComponents(dateComponents)!

        return nextDate
    }

    func getNextDay(date:NSDate) -> NSDate {
        var method = nextDay
        let tomorrow = method(date)

        return tomorrow
    }
}

![enter image description here][2]let calendar = NSCalendar.currentCalendar()
let today = NSDate()

let tomorrow = calendar.getNextDay(today)
Owen Zhao
  • 3,205
  • 1
  • 26
  • 43
  • Why are you passing `method` a parameter if it is a variable and not a function? – Ian Feb 01 '15 at 07:20
  • Because method is nextDay, so calling method(date) is the same as calling nextDay(date). Isn't it? – Owen Zhao Feb 01 '15 at 08:36
  • 1
    No. See this: http://stackoverflow.com/questions/24088312/does-swift-not-work-with-function-pointers – Ian Feb 01 '15 at 08:47
  • why not just `return nextDay(date)` – Ian Feb 01 '15 at 08:49
  • I'm actually somewhat confused as to why you have two functions that take the same parameter, return the same type, and are named basically the same thing indicating that they should do the same thing – Ian Feb 01 '15 at 08:51
  • I am using the getNextDay(NSDate) function as it is dynamic and to show the problem I have. As I may have many methods like (NSDate) -> (NSDate), so I can call them in getNextDay() dynamically by switch-case. – Owen Zhao Feb 01 '15 at 09:02
  • Well if you want to do that, you need to convert your `nextDay` function into a closure – Ian Feb 01 '15 at 09:03
  • Thank you. I think I know the direction I need to go to. – Owen Zhao Feb 01 '15 at 09:05

0 Answers0