简体   繁体   中英

MonthYearPickerView Library Swift 2.0 Issue

I'm using MonthYearPickerView Library in my project and it is running fine in Swift 1.2 now I'm trying to convert it into Swift 2.0 and I'm getting some issues.

I changed the calendar components into the code below under the commented code but I have an issue Cannot convert value of type 'NSDateComponents' to expected argument type 'Int' when I'm trying to append or minus the value of the year. So please how could I fix this issue?

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)
    }

Use component function not components : var year = calendar.component(.Year, fromDate:NSDate())

or var year = calendar.components(.Year, fromDate:NSDate()).year

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM