executeFetchRequest抛出致命错误:NSArray的元素眼福雨燕数组元素类型 [英] executeFetchRequest throw fatal error: NSArray element failed to match the Swift Array Element type

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

问题描述

我测试与CoreData迅速,我创建了以下code:

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

当我试图从数组的值:

 let firstContact = result[0] as Contact

我想这个问题是在 executeFetchRequest 。在的Objective-C executeFetchRequest 的返回类型是的NSArray 。现在,在斯威夫特的返回类型是 [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 ,但X code。所述?

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

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

我在做什么错了?

What I'm doing wrong ?

我使用:
X code ++ 6.0(6A313)和通用汽车
OSX 10.9.4

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

更新:结果
-----------

如果我得到 executeFetchRequest 结果作为一个可选的阵列,并使用 [NSManagedObject] 而不是 [联系方式] 它的工作原理。

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)")
    }

UPDATE2:结果
-----------

在实体实例没有得到使用NSManagedObject类加载发生这种问题。有几个不同的原因出现这种情况,所有这一切都集中在核心数据找不到你在数据模型中的类行的实体指定的类。

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."

其中的可能性:结果
- 您指定了错误的包为您雨燕类结果
- 你忘了指定类结果
- 你拼错了名字结果
结果
在我来说,是我忘了指定类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).

一旦这样做,你将能够使用与你原来的code联系子类。您可以了解更多关于核心数据和命名空间<一个href=\"https://developer.apple.com/library/$p$prelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html\">here.

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的元素眼福雨燕数组元素类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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