I am trying to display items in the UIPickerView from CoreData. But I always get a NSArray element failed to match the Swift Array Element type
inside pickerviews titleforrow function. Here's my complete code:
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]
}
}
What I am trying to do back then is to fetch some records from Coredata and put them in an Array just like what you see in my variable declarations. But I receive an error AnyObject cannot be converted to String
.
Sorry for this simple question. I am just new in Swift and iOS dev.
EDIT:
I get the error from this line: return company[a-1].name! as String
Here's my Company class:
import Foundation
import CoreData
class Company: NSManagedObject {
// Insert code here to add functionality to your managed object subclass
}
xcmodeld:
I just noticed this text from the output window: Unable to load class named 'Company' for entity 'Company'. Class not found, using default NSManagedObject instead.