NSArray不匹配Swift数组元素类型

时间:2016-03-07 15:26:56

标签: ios swift core-data

我正在尝试从CoreData显示UIPickerView中的项目。但我总是在pickerviews titleforrow函数中获得NSArray element failed to match the Swift Array Element type。这是我的完整代码:

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。

2 个答案:

答案 0 :(得分:2)

首先将数据模型文件中类Module的{​​{1}}设置为Company

Current Product Module中的属性name似乎被声明为可选Company,并且由于该方法还返回了一个可选的String,它可能只是< / p>

String

答案 1 :(得分:1)

as强制转换只能用于保证成功的强制转换(因为我认为是Swift 1.2)。如果你真的想强制演员,因为你知道它会有效,你可以使用as!但我总是在这些情况下使用as?,然后处理返回的可选项。

你可以改变:

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

为:

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

因为该方法无论如何都会返回一个可选项。如果它没有用nil-coalescing来处理nil的情况:

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

哪里&#34; - &#34;是一个合适的后备。