gpt4 book ai didi

ios - iOS 中的核心数据,索引超出范围

转载 作者:行者123 更新时间:2023-11-28 12:20:04 24 4
gpt4 key购买 nike

在我的应用程序中,我使用的是 Core Data,到目前为止一切正常。我在尝试更新条目时遇到问题。有趣的是,我可以更新实体中除地址字段之外的所有内容。我没有做任何不同的事情,但是当我尝试仅更新该字段并出现 fatal error 时应用程序崩溃:索引超出范围。这是实际实体的屏幕截图。

enter image description here

这只是一些客户端信息,所有条目都是字符串。在我编辑的 vc 中,我使用了一个 var 来保存客户端信息,设置委托(delegate)并使用了一些谓词。这是代码:

//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 时,我做同样的事情,但只是从核心数据中读取。这里是详细客户端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 也在该类下定义。我知道索引超出范围与尝试将某些东西放在数组中没有任何东西的地方有关,但我不明白为什么它只在我尝试编辑地址时才这样做。所有其他条目都完美更新。

编辑 2017 年 7 月 19 日

感谢大家的回复! Maor 和 Jay 的谓词都是对的。我只是在客户端上搜索地址,而不是前三个属性。在 Jay 关于 var 的注释之后,我能够传回新数据并更新 var 并搜索新记录。再次感谢!

最佳答案

我认为您遇到的问题与您认为的不同。首先,当你写:

fetchRequest.predicate = NSPredicate(format: "lastName == %@", myLastName)
fetchRequest.predicate = NSPredicate(format: "firstName == %@", myFirstName)
fetchRequest.predicate = NSPredicate(format: "address == %@", myAddress)

只有最后一行被考虑在内,因为它覆盖了它之前的任何内容,这意味着在你的情况下它只会查找地址属性(我猜这就是为什么你认为你的问题只在地址中 field ..)您应该按照@Jay 提供的方法来获得复合谓词。

第二件事(如果我理解正确的话)是您将地址更改为 myAddressLabel.text 包含的任何文本,并保存了它。但是,当您从 detail vc 上的核心数据中读取时,您仍然尝试通过“myAdress”变量进行查询,这与您保存的变量不同。如果是这种情况,它会解释为什么您会收到此错误消息 - 它只是没有找到与您的搜索匹配的任何内容,因此当您尝试从空数组中获取第一个元素时,它会告诉您它已超出界限...

关于ios - iOS 中的核心数据,索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45165351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com