2016-03-07 55 views
1

我想從CoreData中顯示UIPickerView中的項目。但我總是得到一個NSArray element failed to match the Swift Array Element type裏面pickerviews的titleforrow函數。這是我的完整代碼:NSArray不匹配Swift數組元素類型

import Foundation 
import UIKit 
import CoreData 

class Create_New: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{ 
    var picker = UIPickerView() 
    var picker2 = UIPickerView() 
    var territory: [String] = [] 
    var company: [Company] = [] 
    var facility: [String] = [] 
    var specialty: [String] = [] 
    var a = 0, b = 0, c = 0, d = 0 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     picker.delegate = self 
     picker.dataSource = self 

     picker2.delegate = self 
     picker2.dataSource = self 

    } 
    override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(animated) 

     var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate) 
     var context:NSManagedObjectContext = appDel.managedObjectContext 
     let fetchRequest = NSFetchRequest(entityName:"Company") 
     let fetchRequestTerritory = NSFetchRequest(entityName:"Territory") 
     let fetchRequestFacility = NSFetchRequest(entityName:"Facility") 
     let fetchRequestSpecialty = NSFetchRequest(entityName:"Specialty") 
     let error:NSError 

     do { 
      let company_temp = try context.executeFetchRequest(fetchRequest) 

      company = company_temp as! [Company] 
      a = company_temp.count 
      print(company.count) 

      let territory_temp = try context.executeFetchRequest(fetchRequestTerritory) 
      territory = territory_temp.flatMap{ $0 as? String } 
      b = territory_temp.count 
      print(territory_temp.count) 

      let facility_temp = try context.executeFetchRequest(fetchRequestFacility) 
      facility = facility_temp.flatMap{ $0 as? String } 
      c = facility_temp.count 
      print(facility_temp.count) 

      let specialty_temp = try context.executeFetchRequest(fetchRequestSpecialty) 
      specialty = specialty_temp.flatMap{ $0 as? String } 
      d = specialty.count 
      print(specialty_temp.count) 

     } catch let error as NSError { 
      // failure 
      print("Fetch failed: \(error.localizedDescription)") 
     } 


     } 
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
     return 1 
    } 

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
     var x = 1 
     if (pickerView.tag == 1){ 
      //print(territory.count) 
      return b 
     }else if (pickerView.tag == 2){ 
      //print(company.count) 
      return a 
     }else if (pickerView.tag == 3){ 
      //print(facility.count) 
      return c 
     }else if (pickerView.tag == 4){ 
      //print(specialty.count) 
      return d 
     } 
     return x 
    } 

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 


    } 
    func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { 
     return 200 
    } 

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
     var temp: [String] = [] 
       if (pickerView.tag == 1){ 
      return company[a-1].name! as String 
     }else if (pickerView.tag == 2){ 
      return company[a-1].name 
     }else if (pickerView.tag == 3){ 
      return company[a-1].name 
     }else if (pickerView.tag == 4){ 
      return company[a-1].name 
     } 
     return temp[row] 
    } 
} 

我所試圖做的當時是從Coredata獲取一些記錄,並把它們放在一個陣列,就像你在我的變量聲明見。但我收到一個錯誤AnyObject cannot be converted to String。 對不起,這個簡單的問題。我只是在Swift和iOS開發中的新人。

編輯: 我從該行得到的錯誤:return company[a-1].name! as String

這是我的公司類:

import Foundation 
import CoreData 


class Company: NSManagedObject { 

// Insert code here to add functionality to your managed object subclass 

} 

xcmod​​eld:

enter image description here

我只注意到這個文本從輸出窗口: 無法爲實體「公司」加載名爲「公司」的類。找不到類,使用默認的NSManagedObject來代替。

+0

變更公司[a-1] .name! as String to company [a-1] .name as!字符串 – ogres

+0

我收到錯誤:'從'字符串Downcast'?以'字符串'僅解開可選項;你的意思是使用'!'?' –

+0

好吧,然後把!在行結尾處,如公司[a-1] .name as String! – ogres

回答

2

首先設置在數據模型文件中的類CompanyModuleCurrent Product Module

酒店nameCompany似乎被聲明爲可選String,由於該方法也返回一個可選String,它可能只需

return company[a-1].name 
+0

同樣的錯誤仍然 –

+0

我更新了答案。 – vadian

+0

謝謝!拯救生命!模塊部分是解決方案!謝謝!!! –

1

as強制轉換隻能用於保證成功的轉換(因爲我認爲Swift是1.2)。如果你真的想強制演員,因爲你知道這將是有效的,你可以使用as!,但我總是在這些情況下使用as?,然後處理可選返回。

你可以改變:

return company[a-1].name! as String 

到:

return company[a-1].name as? String 

,因爲該方法返回反正可選。如果沒有,您可以使用無合併處理零案例:

return company[a-1].name as? String ?? "-" 

其中「 - 」是合適的後備。

+0

我收到一個錯誤消息:'從'String'下降了嗎?'以'字符串'僅解開可選項;你的意思是使用'!'嗎?'我可能會使用Swift 2 –

+0

我以爲它是基於前一個錯誤的AnyObject。 (你會在Swift 2上。我只注意到它是從一個老教程中提出的。) –

+0

我認爲vadian的答案更符合目標。我認爲關於無法加載課程的說明是關鍵信息。 –

相關問題