【发布时间】:2017-12-23 05:41:38
【问题描述】:
在我的应用程序中,我使用的是 Core Data,到目前为止一切正常。当我尝试更新条目时遇到问题。有趣的是,我可以更新实体中除地址字段之外的所有内容。我没有做任何不同的事情,但是当我尝试仅更新该字段并出现致命错误时应用程序崩溃:索引超出范围。这是实际实体的屏幕截图。
这只是一些客户信息,所有条目都是字符串。在我的编辑 vc 中,我使用了一个 var 来保存客户端信息、设置委托并使用了一些谓词。代码如下:
//This is declared right under the class
var myClientToEdit: NSManagedObject = NSManagedObject()
//my save button action
@IBAction func saveMyEdits(_ sender: Any)
{
//get the delegate
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else
{
return
}
//get managedContext
let managedContext = appDelegate.persistentContainer.viewContext
//get the client's entry
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Clients")
fetchRequest.returnsObjectsAsFaults = false
//use predicates to make sure it is the client I need
fetchRequest.predicate = NSPredicate(format: "lastName == %@", mylastName)
fetchRequest.predicate = NSPredicate(format: "firstName == %@", myfirstName)
fetchRequest.predicate = NSPredicate(format: "address = %@", myaddress)
do {
var myClientToEdit = try managedContext.fetch(fetchRequest)
let myObject = myClientToEdit[0]
//my correct client is printed
print(myObject)
//using new entries to set new values
myObject.setValue(lastName.text!, forKey: "lastName")
myObject.setValue(firstName.text!, forKey: "firstName")
myObject.setValue(address.text!, forKey: "address")
myObject.setValue(city.text!, forKey: "city")
myObject.setValue(state.text!, forKey: "state")
myObject.setValue(zip.text!, forKey: "zipCode")
myObject.setValue(phoneDay.text!, forKey: "phoneDay")
myObject.setValue(phoneEve.text!, forKey: "phoneEvening")
myObject.setValue(email.text!, forKey: "email")
do{
//save
try managedContext.save()
}catch let error as NSError{
print(error)
}
}catch let error as NSError {
print(error)
}
//dismis upon saving edits
self.dismiss(animated: true, completion: nil)
}
当我回到我的详细客户端 vc 时,我会做同样的事情,但只是从 Core Data 中读取。这是详细客户端vc的代码。
override func viewWillAppear(_ animated: Bool)
{
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else
{
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Clients")
fetchRequest.predicate = NSPredicate(format: "lastName == %@", myLastName)
fetchRequest.predicate = NSPredicate(format: "firstName == %@", myFirstName)
fetchRequest.predicate = NSPredicate(format: "address == %@", myAddress)
do {
var thisIsTheClient = try managedContext.fetch(fetchRequest)
let myObject = thisIsTheClient[0]
print(myObject)
lastNameLabel.text = myObject.value(forKey: "lastName") as? String
firstNameLabel.text = myObject.value(forKey: "firstName") as? String
myCityLabel.text = myObject.value(forKey: "city") as? String
myAddressLabel.text = myObject.value(forKey: "address") as? String
myStateLabel.text = myObject.value(forKey: "state") as? String
myZipLabel.text = myObject.value(forKey: "zipCode") as? String
myDayLabel.text = myObject.value(forKey: "phoneDay") as? String
myEveLabel.text = myObject.value(forKey: "phoneEvening") as? String
myEmailLabel.text = myObject.value(forKey: "email") as? String
}catch let error as NSError
{
print(error)
}
}
错误就行了:let myObject = thisIsTheClient[0]。但只有当我尝试编辑地址时。 thisIsTheClient 也在类下定义。我知道 index out of range 与尝试将某些东西放在数组中没有类似的地方有关,但我不明白为什么它只在我尝试编辑地址时才会这样做。所有其他条目都已完美更新。
编辑 7/19/17
感谢大家的回复! Maor 和 Jay 都有正确的谓词。我只是在客户端上搜索地址,而不是前三个属性。在 Jay 关于 var 的注释之后,我能够将新数据传回并更新 var 并搜索新记录。再次感谢!
【问题讨论】:
-
你试过调试 SQL 操作吗?看起来您的带地址的 SQL 操作出现了一些错误。调试 SQL 操作编辑您的方案并在参数部分添加以下代码 -com.apple.CoreData.Logging.stderr 1
-
您是否在已经存储了一些数据时添加了地址属性?
-
@Roy,是的,地址以及所有其他属性都有值。我正在编辑 vc 中重写它们
-
如果是,那么您需要重置 Clients 的数据,然后您可以随时更新地址@Douglas