ios - NSArray 不匹配 Swift 数组元素类型

标签 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!作为字符串

这是我的公司类:

import Foundation
import CoreData


class Company: NSManagedObject {

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

}

xc模型:

enter image description here

我刚从输出窗口注意到这段文字: 无法为实体“公司”加载名为“公司”的类。找不到类,改用默认的 NSManagedObject。

最佳答案

首先将数据模型文件中Company类的Module设置为Current Product Module

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

return company[a-1].name

关于ios - NSArray 不匹配 Swift 数组元素类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35847518/

相关文章:

ios - 将多个图层剪切到一个设置的框架? (核心动画/Quartz 2d)

ios - 如何使用 Swift 编辑 iOS 日历应用程序中的事件日期?

iphone - 如何提高 iPhone 上的核心数据获取性能?

ios - 核心数据保存错误

iphone - 如何使用 CGPDFDictionaryRef 对象引用知道 PDF 文件名

ios - 自定义 UISearchBar 结果单元格

objective-c - 构建 iOS 静态库

video - Swift Get Video Frame 使用 AVFoundation

iOS插入数据库

iphone - performBlock 在哪个线程上运行?