MonthYearPickerView 库 Swift 2.0 问题
MonthYearPickerView Library Swift 2.0 Issue
我在我的项目中使用 MonthYearPickerView 库,它在 Swift 1.2 中 运行 很好,现在我正在尝试将其转换为 Swift 2.0 和我遇到了一些问题。
我在注释代码下将日历组件更改为下面的代码,但是当我尝试追加或减去年份值时遇到问题 Cannot convert value of type 'NSDateComponents' to expected argument type 'Int'
。那么请问我该如何解决这个问题?
class MonthYearPickerView: UIPickerView, UIPickerViewDelegate, UIPickerViewDataSource {
override init(frame: CGRect) {
var years: [Int] = []
if years.count == 0 {
//var year = NSCalendar(identifier: NSCalendarIdentifierGregorian)!.component(.CalendarUnitYear, fromDate: NSDate())
let calendar = NSCalendar.currentCalendar()
var year = calendar.components(.Year, fromDate:NSDate())
for i in 1...10 {
years.append(year)
year++
}
}
self.years = years
super.init(frame: frame)
self.delegate = self
self.dataSource = self
//var month = NSCalendar(identifier: NSCalendarIdentifierGregorian)!.component(.CalendarUnitMonth, fromDate: NSDate())
let calendar = NSCalendar.currentCalendar()
let month = calendar.components(.Month, fromDate:NSDate())
self.selectRow(month-1, inComponent: 0, animated: false)
}
使用 component
函数而不是 components
:
var year = calendar.component(.Year, fromDate:NSDate())
或
var year = calendar.components(.Year, fromDate:NSDate()).year
我在我的项目中使用 MonthYearPickerView 库,它在 Swift 1.2 中 运行 很好,现在我正在尝试将其转换为 Swift 2.0 和我遇到了一些问题。
我在注释代码下将日历组件更改为下面的代码,但是当我尝试追加或减去年份值时遇到问题 Cannot convert value of type 'NSDateComponents' to expected argument type 'Int'
。那么请问我该如何解决这个问题?
class MonthYearPickerView: UIPickerView, UIPickerViewDelegate, UIPickerViewDataSource {
override init(frame: CGRect) {
var years: [Int] = []
if years.count == 0 {
//var year = NSCalendar(identifier: NSCalendarIdentifierGregorian)!.component(.CalendarUnitYear, fromDate: NSDate())
let calendar = NSCalendar.currentCalendar()
var year = calendar.components(.Year, fromDate:NSDate())
for i in 1...10 {
years.append(year)
year++
}
}
self.years = years
super.init(frame: frame)
self.delegate = self
self.dataSource = self
//var month = NSCalendar(identifier: NSCalendarIdentifierGregorian)!.component(.CalendarUnitMonth, fromDate: NSDate())
let calendar = NSCalendar.currentCalendar()
let month = calendar.components(.Month, fromDate:NSDate())
self.selectRow(month-1, inComponent: 0, animated: false)
}
使用 component
函数而不是 components
:
var year = calendar.component(.Year, fromDate:NSDate())
或
var year = calendar.components(.Year, fromDate:NSDate()).year