为什么 Swift 不接受这个 NSCalendar 方法调用?

Why will Swift not accept this NSCalendar method call?

let dateComponents = NSCalendar.currentCalendar().components(.MonthCalendarUnit, fromDate: dateCreated, toDate: NSDate(), options: 0)

我试图在几个月内找出两个 NSDate 之间的差异,但此功能 合作。它一直抱怨 "extra argument 'toDate' in call",但它肯定应该在那里。

extension NSDate {
    func monthsFrom(date:NSDate) -> Int{
        return NSCalendar.currentCalendar().components(.CalendarUnitMonth, fromDate: date, toDate: self, options: nil).month
    }
}

let dateX = NSCalendar.currentCalendar().dateWithEra(1, year: 2014, month: 11, day: 28, hour: 5, minute: 9, second: 0, nanosecond: 0)!
let dateY = NSCalendar.currentCalendar().dateWithEra(1, year: 2015, month: 1, day: 1, hour: 22, minute: 51, second: 0, nanosecond: 0)!

let months = dateY.monthsFrom(dateX)  // 1

Swift 不接受 0 作为 options 的值,如 Objective-C。使用 nil 代替:

let dateComponents = NSCalendar.currentCalendar().components(.MonthCalendarUnit, fromDate: dateCreated, toDate: NSDate(), options: nil)

然后你所要做的就是通过 dateComponents.month.

得到月数