NSFetchRequest找不到NSEntityDescription

时间:2014-12-17 23:06:29

标签: xcode core-data swift parse-platform

我知道这是其他问题的重复,但我已经按照这些问题的答案进行了解答,但仍然会遇到同样的错误。

我认为错误来自这样一个事实,即代码在没有保存时​​试图获取数据(由于我从Parse获取数据,因此保存数据有延迟)。有什么想法吗?

我得到的确切错误是:

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSFetchRequest could not locate an NSEntityDescription for entity name 'emailStatus''

-

import UIKit
import CoreData

class RegisterEmail: UIViewController {

var test1: Bool?

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
    if identifier == "passEmail" {

        var query = PFUser.query()
        query.getObjectInBackgroundWithId("vFu93HatwL") {
            (User: PFObject!, error: NSError!) -> Void in
            if error == nil {
                NSLog("%@", User)

                var checkEmail = User["emailVerified"] as Bool

                println(checkEmail)

                var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)

                var context:NSManagedObjectContext = appDel.managedObjectContext!

                var newEmail = NSEntityDescription.insertNewObjectForEntityForName("Email", inManagedObjectContext: context) as NSManagedObject

                newEmail.setValue(checkEmail, forKey: "emailStatus")

                context.save(nil)

                println (newEmail)

                println("Object Saved")

            } else {
                NSLog("%@", error)
            }


        }


        var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)

        var context:NSManagedObjectContext = appDel.managedObjectContext!

        var request = NSFetchRequest(entityName: "emailStatus")

        request.returnsObjectsAsFaults = false

        var results: NSArray = context.executeFetchRequest(request, error: nil)!

        if(results.count > 0)
        {
            var res = results [0] as NSManagedObject

            test1 = res.valueForKey("emailStatus") as Bool

        }


        if (test1 == false) {

            let alert = UIAlertView()
            alert.title = "Error"
            alert.message = "The email you have provided has not been verified."
            alert.addButtonWithTitle("Dismiss")
            alert.show()

            return false
        }

        else {

            return true
        }
    }

    // by default, transition
    return false
}
}

1 个答案:

答案 0 :(得分:5)

来自op:“......一切都在数据库文件中设置。实体称为电子邮件,属性为emailStatus”

但您尝试获取实体emailStatus,您可能需要电子邮件。
var request = NSFetchRequest(entityName: "emailStatus")

'错字'

获取正确的实体:'email'
var request = NSFetchRequest(entityName: "email")