Reputation: 2811
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.
Upvotes: 1
Views: 1064
Reputation: 285072
First of all set the Module
of the class Company
in the data model file to Current Product Module
The property name
in Company
seems to be declared as optional String
, and since the method returns also an optional String
, it's probably simply
return company[a-1].name
Upvotes: 2
Reputation: 6504
as
casts can only be used for casts guaranteed to succeed (since Swift 1.2 I think). If you really want to force a cast because you know it will be valid you can use as!
but I always use as?
in these situations and then handle the optional returned.
You could change:
return company[a-1].name! as String
to:
return company[a-1].name as? String
because the method returns an optional anyway. If it didn't you could handle the nil case with nil-coalescing:
return company[a-1].name as? String ?? "-"
Where "-" is a suitable fallback.
Upvotes: 1