我正在创建一个可以保存核心数据文件中人名的应用程序,当我运行应用程序并保存用户输入的内容时,当我加载(获取)数据时,它也能正常工作,问题来了当我点击加载数据(获取)时关闭或转到不同的视图控制器时,应用程序崩溃:'NSInvalidArgumentException', 原因:'executeFetchRequest:错误: 不是有效的NSFetchRequest。'
这是我的代码
@IBOutlet var PresidentTxt: UITextField!
@IBOutlet var SecDefenseTxt: UITextField!
let dataStack = CoreDataStack()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Save(_ sender: Any) {
let database = dataStack.persistenContainer.viewContext
let dbrRecord = Command(context: database)
dbrRecord.potus = PresidentTxt.text
dbrRecord.defsec = SecDefenseTxt.text
do{
try database.save()
}catch {}
}
func didTapView(){
self.view.endEditing(true)
}
@IBAction func Load(_ sender: Any) {
let database = dataStack.persistenContainer.viewContext
do{
let results = try database.fetch(Command.fetchRequest()) as! [Command]
for presidentUS in results{
if let _ = presidentUS.potus {
PresidentTxt.text = presidentUS.potus
}
}
for DefenseSec in results {
if let _ = DefenseSec.defsec {
SecDefenseTxt.text = DefenseSec.defsec
}
}
}catch {}
}