executeFetchRequest 抛出致命错误:NSArray 元素未能匹配 Swift 数组元素类型 [英] executeFetchRequest throw fatal error: NSArray element failed to match the Swift Array Element type

查看:29
本文介绍了executeFetchRequest 抛出致命错误:NSArray 元素未能匹配 Swift 数组元素类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CoreData 测试 swift 并创建了以下代码:

I'm testing swift with CoreData and I created the following code:

import UIKit
import CoreData

class Contact: NSManagedObject {

    @NSManaged var name: String
    @NSManaged var email: String

    class func execute(){
        let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
        let context:NSManagedObjectContext = appDel.managedObjectContext!

        let entityDescripition = NSEntityDescription.entityForName("Contact", inManagedObjectContext: context)
        let contact = Contact(entity: entityDescripition!, insertIntoManagedObjectContext: context)

        contact.name = "john Apleesee"
        contact.email = "john@apple.com"

        context.save(nil)

        let fetch:NSFetchRequest = NSFetchRequest(entityName: "Contact")
        var result = context.executeFetchRequest(fetch, error: nil) as [Contact]

        let firstContact = result[0] as Contact  //  <<-- error !

        println("\(firstContact.name)")
        println("\(firstContact.email)")

    }

}

当我跑步时:

Contact.execute()

然后编译器抛出这个错误:

Then the compiler throw this error:

fatal error: NSArray element failed to match the Swift Array Element type

当我尝试从数组中获取值时:

when I try to get values from array:

 let firstContact = result[0] as Contact

我猜问题出在executeFetchRequest.在 Objective-c 上,executeFetchRequest 的返回类型是 NSArray.现在在 Swift 上,返回类型是 [AnyObject]?.

I guess the problem is in executeFetchRequest. On Objective-c the return type of executeFetchRequest is a NSArray. Now on Swift the return type is a [AnyObject]?.

我试图将 [AnyObject]? 结果转换为 NSArray,但 Xcode 说:

I tried to cast the [AnyObject]? result to NSArray, but Xcode said:

 Cannot convert the expression's type '[AnyObject]?' to type 'NSArray' 

我做错了什么?

我正在使用:Xcode 6.0 (6A313) 通用和OSX 10.9.4

I'm using: Xcode 6.0 (6A313) GM and OSX 10.9.4

更新:
-----------

如果我得到 executeFetchRequest 结果作为一个可选数组并使用 [NSManagedObject] 而不是 [Contact] 它可以工作.

If I get executeFetchRequest result as an optional array and use [NSManagedObject] instead of [Contact] it works.

if let result: [NSManagedObject]? = context.executeFetchRequest(fetch, error: nil) as? [NSManagedObject] {

        let firstContact: NSManagedObject = result![0]

        let name = firstContact.valueForKey("name") as String
        let email = firstContact.valueForKey("email") as String

        println("\(name)")
        println("\(email)")
    }

更新 2:
-----------

当实体实例没有使用您的 NSManagedObject 类加载时,就会出现这种问题.发生这种情况的原因有多种,所有这些原因都集中在Core Data 无法在数据模型的类行中找到您为该实体指定的类."

This kind of problem occurs when entity instances didn't get loaded using your NSManagedObject class. There are a few different reasons why that happens, all of which focus on "Core Data couldn't find the class you specified for that entity on the class line in the data model."

可能性之一:
- 您为 Swift 类指定了错误的包
- 您忘记指定类别
- 你拼错了名字

就我而言,我忘记指定 POB 在她的回答中显示的类.

Among the possibilities:
- You specified the wrong package for your Swift class
- You forgot to specify the class
- You misspelled the name

In my case was I forgot to specify the class as POB shows in her answer.

推荐答案

我能够在控制台中重现您的错误消息.您收到此消息是因为您没有在核心数据模型编辑器中正确设置实体类名称(见下图).

I was able to reproduce your error message in the console. You get this message because you didn't set correctly your entity class name in the Core Data Model Editor (see image below).

完成后,您将能够使用带有 Contact 子类的原始代码.您可以了解有关 Core Data 和命名空间的更多信息 这里.

Once done, you will be able to use your original code with Contact subClass. You can learn more about Core Data and namespaces here.

这篇关于executeFetchRequest 抛出致命错误:NSArray 元素未能匹配 Swift 数组元素类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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