更新核心数据上的对象 swift 4
Update an object on core data swift 4
我在核心数据实体中有几项作业,其作业 ID 为 -1。我需要获取所有这些项目并通过在 updateMyJobs 方法中传递的对象中的适当 ID 更新 jobId。我还没有提取 NSManagedObject class 来处理核心数据(即-我已经检查了实体作为 Class 定义)
这是我的代码:
func updateMyJobs(jobId: Int){
managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "DBJobsNearBy")
fetchRequest.predicate = NSPredicate(format: "jobId = '-1'")
let result = try? managedObjectContext.fetch(fetchRequest)
let resultData = result as! [DBJobsNearBy]
for object in resultData {
print(object.jobId)
if object.jobId == -1 {
object.setValue("\(jobId)", forKey: "jobId")
}
}
do {
try managedObjectContext.save()
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
我传递了一些整数值并调用上面的方法来更新项目。
DatabaseHandler.shared.updateMyJobs(jobId: 222)
错误:由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'Unacceptable type of value for attribute: property = "jobId"; desired type = NSNumber; given type = Swift._NSContiguousString; value = 222.'
我正在尝试将新的 jobId 设置为核心数据实体中的相关对象。是否需要提取 NSManagedObject Class 或不需要。请有人帮我做这件事。谢谢。
请阅读错误信息。这很清楚。在 setValue
中,您传递的是 String
(通过字符串插值)而不是预期的 Int
或 NSNumber
object.setValue(jobId, forKey: "jobId")
或
object.setValue(NSNumber(value: jobId), forKey: "jobId")
但最好和推荐的方式是点符号
object.jobId = jobId
您需要设置 NSNumber
而不是 String
。替换此行:
object.setValue("\(jobId)", forKey: "jobId")
与:
object.jobId = jobId
将对象更新为核心数据
@IBAction func buttonUpdate(_ sender: Any) {
let entity = NSEntityDescription.entity(forEntityName: "Students", in: managedContext)
let request = NSFetchRequest<NSFetchRequestResult>()
request.entity = entity
let newName = UserDefaults.standard.value(forKey: "new") as! String
let predicate = NSPredicate(format: "(name = %@)", newName)
request.predicate = predicate
do {
let results =
try managedContext.fetch(request)
let objectUpdate = results[0] as! NSManagedObject
objectUpdate.setValue(txtName.text!, forKey: "name")
objectUpdate.setValue(txtPhone.text!, forKey: "phone")
objectUpdate.setValue(txt_Address.text!, forKey: "address")
do {
try managedContext.save()
labelStatus.text = "Updated"
}catch let error as NSError {
labelStatus.text = error.localizedFailureReason
}
}
catch let error as NSError {
labelStatus.text = error.localizedFailureReason
}
}
我在核心数据实体中有几项作业,其作业 ID 为 -1。我需要获取所有这些项目并通过在 updateMyJobs 方法中传递的对象中的适当 ID 更新 jobId。我还没有提取 NSManagedObject class 来处理核心数据(即-我已经检查了实体作为 Class 定义)
这是我的代码:
func updateMyJobs(jobId: Int){
managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "DBJobsNearBy")
fetchRequest.predicate = NSPredicate(format: "jobId = '-1'")
let result = try? managedObjectContext.fetch(fetchRequest)
let resultData = result as! [DBJobsNearBy]
for object in resultData {
print(object.jobId)
if object.jobId == -1 {
object.setValue("\(jobId)", forKey: "jobId")
}
}
do {
try managedObjectContext.save()
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
我传递了一些整数值并调用上面的方法来更新项目。
DatabaseHandler.shared.updateMyJobs(jobId: 222)
错误:由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'Unacceptable type of value for attribute: property = "jobId"; desired type = NSNumber; given type = Swift._NSContiguousString; value = 222.'
我正在尝试将新的 jobId 设置为核心数据实体中的相关对象。是否需要提取 NSManagedObject Class 或不需要。请有人帮我做这件事。谢谢。
请阅读错误信息。这很清楚。在 setValue
中,您传递的是 String
(通过字符串插值)而不是预期的 Int
或 NSNumber
object.setValue(jobId, forKey: "jobId")
或
object.setValue(NSNumber(value: jobId), forKey: "jobId")
但最好和推荐的方式是点符号
object.jobId = jobId
您需要设置 NSNumber
而不是 String
。替换此行:
object.setValue("\(jobId)", forKey: "jobId")
与:
object.jobId = jobId
将对象更新为核心数据
@IBAction func buttonUpdate(_ sender: Any) {
let entity = NSEntityDescription.entity(forEntityName: "Students", in: managedContext)
let request = NSFetchRequest<NSFetchRequestResult>()
request.entity = entity
let newName = UserDefaults.standard.value(forKey: "new") as! String
let predicate = NSPredicate(format: "(name = %@)", newName)
request.predicate = predicate
do {
let results =
try managedContext.fetch(request)
let objectUpdate = results[0] as! NSManagedObject
objectUpdate.setValue(txtName.text!, forKey: "name")
objectUpdate.setValue(txtPhone.text!, forKey: "phone")
objectUpdate.setValue(txt_Address.text!, forKey: "address")
do {
try managedContext.save()
labelStatus.text = "Updated"
}catch let error as NSError {
labelStatus.text = error.localizedFailureReason
}
}
catch let error as NSError {
labelStatus.text = error.localizedFailureReason
}
}