Swift数组不能向下转换为派生数组 [英] Swift array cannot be downcast to array of derived

查看:124
本文介绍了Swift数组不能向下转换为派生数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift中使用Core Data,并且无法存储从获取请求返回的数组。我在我的数据模型中有两个实体:任务和家庭作业。家庭作业的父实体是任务。我自动为这两个实体生成类:



Task.swift

  import Foundation 
import CoreData

class任务:NSManagedObject {

@NSManaged var name:String
@NSManaged var due:NSDate
@NSManaged var subject:Subject

}

Homework.swift



  import Foundation 
import CoreData

class Homework:Task {
}

在我的视图控制器中,在查看是否加载,我有以下代码。

  var错误:NSError? = nil 
let fetchRequest = NSFetchRequest()
let entity = NSEntityDescription.entityForName(Task,inManagedObjectContext:managedObjectContext)
fetchRequest.entity = entity
tasks = managedObjectContext.executeFetchRequest fetchRequest,error:& error)as [Task]

  var tasks:[Task] = [] 

但是,当我运行应用程序时,我在将数组分配给任务的行上出现以下运行时错误: fatal error:array can not如果我使用 var temp = managedObjectContext.executeFetchRequest(fetchRequest)替换赋值行,则可以使用


$ b <错误:&错误)作为[NSManagedObject]
,错误不会发生,但我需要将其保存为任务数组,以便能够使用我生成的自定义类。

解决方案

您必须在模型编辑器中将模块或应用程序名称添加到类名称: / p>

  MyApp.Task 

这必须在之后生成类文件,否则该部分失败。我认为这是目前的测试版中的一个错误,并提出了它。



我以前的答案声称使用@objc装饰器,这也工作,但上述解决方案记录在swift /可可参考。


I am using Core Data in Swift and am having trouble storing the array returned from a fetch request. I have two entities in my data model: Task and Homework. Homework's parent entity is Task. I auto generated the classes for the two entities:

Task.swift

import Foundation
import CoreData

class Task: NSManagedObject {

    @NSManaged var name: String
    @NSManaged var due: NSDate
    @NSManaged var subject: Subject

}

Homework.swift

import Foundation
import CoreData

class Homework: Task {
}

In my view controller, in view did load, I have the following code.

var error: NSError? = nil
let fetchRequest = NSFetchRequest()
let entity = NSEntityDescription.entityForName("Task", inManagedObjectContext: managedObjectContext)
fetchRequest.entity = entity
tasks = managedObjectContext.executeFetchRequest(fetchRequest, error: &error) as [Task]

Earlier in the view controller, I initialized tasks:

var tasks : [Task] = []

However, when I run the the application, I get the following runtime error at the line where I assign the array to tasks: fatal error: array cannot be downcast to array of derived

If I replace the assignment line with var temp = managedObjectContext.executeFetchRequest(fetchRequest, error: &error) as [NSManagedObject], the error does not occur, but I need to save it as an array of Tasks in order to be able to use the custom classes that I generated. What am I doing wrong?

解决方案

You have to add the module or app name to the class name in the model editor:

MyApp.Task

This has to be done after you generate the class file, otherwise that part fails. I consider this a bug in the current beta, and have raised it as such.

My previous answer stated to use the @objc decorator, which also works, but the above solution is documented in the swift / cocoa reference.

这篇关于Swift数组不能向下转换为派生数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆