我想從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
}
xcmodeld:
我只注意到這個文本從輸出窗口: 無法爲實體「公司」加載名爲「公司」的類。找不到類,使用默認的NSManagedObject來代替。
變更公司[a-1] .name! as String to company [a-1] .name as!字符串 – ogres
我收到錯誤:'從'字符串Downcast'?以'字符串'僅解開可選項;你的意思是使用'!'?' –
好吧,然後把!在行結尾處,如公司[a-1] .name as String! – ogres