从“NSManagedObject”转换为不相关的类型总是失败

时间:2016-09-14 21:24:48

标签: swift xcode core-data preload

我正在尝试加载数据并将其保存到数据库中,但我在func preloadData()中收到警告和错误:

  

从'NSManagedObject'转换为不相关的类型'LeadItem'总是失败

func removData()

  

无法将'LeadItem'类型的值转换为预期的参数类型'NSManagedObject'

旧版Xcode的类似解决方案对我不起作用。

func preloadData ()
{
    if let contentsOfURL = NSBundle.mainBundle().URLForResource("leads_Data", withExtension: "csv")
    {
        removeData()
        var error:NSError?
        if let leads = parseCSV(contentsOfURL, encoding: NSUTF8StringEncoding, error: &error)
        {
            for lead in leads
                {
                    let leadItem = NSEntityDescription.insertNewObjectForEntityForName("LeadItem", inManagedObjectContext: self.managedObjectContext) as! LeadItem
                }
        }
    }
}

func removeData ()
{
    let fetchRequest = NSFetchRequest(entityName: "LeadItem")
    do
        {
            let leadItems = try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [LeadItem]
            for leadItem in leadItems
            {
                self.managedObjectContext.deleteObject(leadItem)
            }
        }
        catch let error as NSError
        {
            print("Failed to retrieve record: \(error.localizedDescription) \n")

        }
    }
}

0 个答案:

没有答案