简体   繁体   English

executeFetchRequest抛出致命错误:NSArray元素无法匹配Swift数组元素类型

[英]executeFetchRequest throw fatal error: NSArray element failed to match the Swift Array Element type

I'm testing swift with CoreData and I created the following code: 我正在使用CoreData测试swift,我创建了以下代码:

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

    }

}

When I run: 当我跑:

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

I guess the problem is in executeFetchRequest . 我想问题出在executeFetchRequest On Objective-c the return type of executeFetchRequest is a NSArray . Objective-c上, executeFetchRequest的返回类型是NSArray Now on Swift the return type is a [AnyObject]? 现在在Swift上 ,返回类型是[AnyObject]? .

I tried to cast the [AnyObject]? 我试图施放[AnyObject]? result to NSArray , but Xcode said: 结果给NSArray ,但Xcode说:

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

What I'm doing wrong ? 我做错了什么?

I'm using: Xcode 6.0 (6A313) GM and OSX 10.9.4 我正在使用:Xcode 6.0(6A313)GM和OSX 10.9.4

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

If I get executeFetchRequest result as an optional array and use [NSManagedObject] instead of [Contact] it works. 如果我将executeFetchRequest结果作为可选数组并使用[NSManagedObject]而不是[Contact]它就可以工作。

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: UPDATE2:
----------- -----------

This kind of problem occurs when entity instances didn't get loaded using your NSManagedObject class. 当使用NSManagedObject类未加载实体实例时,会发生此类问题。 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." 发生这种情况的原因有几个,所有这些都集中在“核心数据无法找到您在数据模型中的类行上为该实体指定的类”。

Among the possibilities: 其中包括:
- You specified the wrong package for your Swift class - 您为Swift类指定了错误的包
- 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. 在我的情况下,我忘记在她的答案中指定课程作为POB显示。

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). 您收到此消息是因为未在Core Data Model Editor中正确设置实体类名称(请参见下图)。

在此输入图像描述

Once done, you will be able to use your original code with Contact subClass. 完成后,您将能够将原始代码与Contact一起使用。 You can learn more about Core Data and namespaces here . 您可以在此处了解有关Core Data和名称空间的更多信息。

In my case, i changed the Module as "Current Product Module" inside Core Data Model Editor.It started working fine for me. 就我而言,我将模块更改为核心数据模型编辑器中的“当前产品模块”。它开始正常工作。

在此输入图像描述

I think its bad idea to set prefix of your target name like this Because if you would like to move your model into other app, or change targer name you will end up with changing it again, 我认为它的坏主意,设定目标的名字前缀,像这样因为如果你想你的模型转移到其他应用程序,或更改targer名字,你将最终再次改变它,

So there is another way. 所以还有另一种方式。 If you look at how xcode generates core data entities now, as @A_man said you need to set current module in editor and use @objc() like this : 如果你看看xcode现在如何生成核心数据实体,正如@A_man所说,你需要在编辑器中设置当前模块并使用@objc(),如下所示:

import CoreData

@objc(Contact)
class Contact: NSManagedObject {
    @NSManaged var name: String?
    @NSManaged var email: String?
}

And field has to be optional because they can be nil 字段必须是可选的,因为它们可以是零

从Data Model Inspector检查Entity中的类和模块,并将类名称设置为实体名称,将模块设置为“Current Product Module” 类名称和模块

I think your problem is that executeFetchRequest gives back an optional array, not an array. 我认为你的问题是executeFetchRequest返回一个可选数组,而不是数组。 You need to unwrap it. 你需要打开它。

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

    let firstContact = result[0]

    println("/(firstContact.name)")
    println("/(firstContact.email)")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:NSArray元素无法匹配Swift数组元素类型 - fatal error: NSArray element failed to match the Swift Array Element type 使用数组指针和swift 1.2进行PFSubclassing - 致命错误:NSArray元素无法匹配Swift数组元素类型 - PFSubclassing with array pointer and swift 1.2 - fatal error: NSArray element failed to match the Swift Array Element type Firebase iOS Swift致命错误:NSArray元素与Swift Array元素类型不匹配 - Firebase iOS Swift fatal error: NSArray element failed to match the Swift Array Element type 解析:致命错误:NSArray元素与Swift Array元素类型不匹配 - Parse: fatal error: NSArray element failed to match the Swift Array Element type 致命错误NSArray元素在xcode10.1更新后无法与Swift数组元素类型匹配 - Fatal error NSArray element failed to match the Swift Array Element type after xcode10.1 update 线程1:致命错误:NSArray元素无法与Swift数组元素类型匹配 - Thread 1: Fatal error: NSArray element failed to match the Swift Array Element type 在尝试从类对象数组中读取值时,收到“致命错误:NSArray元素无法匹配Swift数组元素类型” - getting “fatal error: NSArray element failed to match the Swift Array Element type” when trying to read values from an array of class objects poet.books引发致命错误:NSArray元素匹配失败 - poet.books throw fatal error: NSArray element failed to match AlamofireJson / EVReflection-NSArray元素无法与以下中的Swift Array Element类型匹配 - AlamofireJson/EVReflection - NSArray element failed to match the Swift Array Element type in NSArray元素的原因无法与Swift数组元素类型匹配 - reason of NSArray element failed to match the Swift Array Element type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM