简体   繁体   English

Swift:由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“实体名称不能为零。”

[英]Swift : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Entity name must not be nil.'

I am trying to implement CoreData with my existing swift project. 我正在尝试使用现有的Swift项目来实现CoreData。 I followed this tutorial and implemented it with UITableView. 我遵循了本教程 ,并使用UITableView实现了它。

The data is getting added contentiously to the data model (from another view controller) when I am fetching data model record (in another view controller). 当我获取数据模型记录(在另一个视图控制器中)时,数据争执地添加到了数据模型(从另一个视图控制器中)。

Problem 问题

Navigating to the View Controller with UITableView, shows data from data model once. 使用UITableView导航到View Controller,一次显示数据模型中的数据。 Tapping on the tableView or sliding it up or down causing the application to crash with error 轻击tableView或向上或向下滑动会导致应用程序崩溃并出现错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Entity name must not be nil.' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“实体名称不能为零。”

Function for saving record : 保存记录功能:

func saveData(date: String, usr : String, piid: String, healthD:String, temp:String, humd:String) {
        //1
        let managedContext = DataController().managedObjectContext

        //2
        let entity =  NSEntityDescription.entityForName("Hygeine",
            inManagedObjectContext:managedContext)

        let health = NSManagedObject(entity: entity!,
            insertIntoManagedObjectContext: managedContext)

        //3
        health.setValue(date, forKey: "date")
        health.setValue(healthD, forKey: "health")
        health.setValue(temp, forKey: "temp")
        health.setValue(humd, forKey: "humd")
        health.setValue(piid, forKey: "pi_id")
        health.setValue(usr, forKey: "usr")
        //4
        do {
            try managedContext.save()
            //5
            healthData.append(health)
        } catch let error as NSError  {
            print("Could not save \(error), \(error.userInfo)")
        }
    }

ViewWillAppear for ViewController with UITableView : 用于带有UITableView的ViewController的ViewWillAppear:

override func viewWillAppear(animated: Bool) {

        let managedContext = DataController().managedObjectContext

        //2
        let fetchRequest = NSFetchRequest(entityName: "Hygeine")

        //3
        do {
            let results =
            try managedContext.executeFetchRequest(fetchRequest)
            healthData = results as! [NSManagedObject]
        } catch let error as NSError {
            print("Could not fetch \(error), \(error.userInfo)")
        }
    }

There are a lot of Objective-C solution to this problem but how to solve it in Swift 2. What I am doing wrong here ? 这个问题有很多Objective-C解决方案,但是如何在Swift 2中解决。

entityForName returns an optional that you are force unwrapping. entityForName返回一个可选的,您可以强制展开。 That is most likely your error and easily caught in the debugger. 这很可能是您的错误,很容易被调试器捕获。

Is your entity name entered wrong? 您的实体名称输入错误吗? Check it against your model. 根据您的模型进行检查。

And as others have said, learn NSFetchedResultsController , while it won't solve this error, it is easier to work with when dealing with a UITableViewController . 正如其他人所说,学习NSFetchedResultsController虽然不会解决此错误,但在处理UITableViewController时更容易使用。

暂无
暂无

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

相关问题 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“数据参数为nil” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'UICollectionView必须使用非nil布局参数初始化 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter **由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'应用程序试图在目标上显示nil模态视图控制器 - ** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ *** setObjectForKey:键不能为零” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“应用程序试图在目标上显示nil模态视图控制器 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target 得到“ ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__ NSCFNumber compare:]:nil参数'” - getting “*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber compare:]: nil argument'” 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ *** setObjectForKey:对象不能为nil(键:索引)” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: index)' 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'+ entityForName:nil不是合法的NSManagedObjectContext - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ ***-[__ NSCFConstantString stringByAppendingString:]:无参数” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString stringByAppendingString:]: nil argument' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:在实体NSSQLEntity CategoryDe​​tail id = 2中找不到“键路径父级” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath parent not found in entity NSSQLEntity CategoryDetail id=2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM