2015-10-07 49 views
0

我在我的項目中使用​​庫,它在Swift 1.2中運行良好,現在我試圖將它轉換爲Swift 2.0,並且出現了一些問題。MonthYearPickerView庫Swift 2.0問題

我將日曆組件更改爲註釋代碼下方的代碼,但當我嘗試附加或減去年份的值時,我遇到了一個問題Cannot convert value of type 'NSDateComponents' to expected argument type 'Int'。那麼請我如何解決這個問題?

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

回答

0

使用component功能不componentsvar year = calendar.component(.Year, fromDate:NSDate())

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