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

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

问题描述

我用CoreData测试swift,我创建了以下代码:

  import UIKit 
import CoreData

类联系人: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)

.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 //< ; - 错误!

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

}
b $ b}

当我运行时:

  Contact.execute()

错误:

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

  

> let firstContact = result [0] as Contact

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



[AnyObject]?结果转换为 NSArray ,但Xcode表示:

 无法将表达式类型'[AnyObject]?'转换为类型'NSArray'

我做错了什么?



我使用的是:
Xcode 6.0(6A313)GM和
OSX 10.9.4



更新:

---------- -



如果我获得 executeFetchRequest 结果作为可选数组并使用 [NSManagedObject] 而不是 [联系] 它工作。

 如果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类加载时,会出现此类问题。有几个不同的原因,为什么会发生这一切,所有的焦点核心数据无法找到您为该实体指定的类在数据模型中的类行。



可能性之间:

- 您为Swift类指定了错误的包

- 您忘记指定类

- 您拼写错误名称



在我的例子中,我忘记在她的回答中指定类POB。

解决方案

我能够在控制台中重现您的错误消息。您收到此消息是因为您未在Core数据模型编辑器中正确设置实体类名称(请参见下面的图片)。





完成后,您就可以使用原始代码联系子类。您可以详细了解Core Data和命名空间这里


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

    }

}

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. On Objective-c the return type of executeFetchRequest is a NSArray. Now on Swift the return type is a [AnyObject]?.

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 ?

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

Update:
-----------

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

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

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

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 throw致命错误:NSArray元素无法与Swift数组元素类型匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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