I'm using CoreData and swift and trying to update an array of NSManagedObjects. However, I am receiving "Value of type '[NSManagedObject]' has no member 'setValue'" when I attempt to update both keys within the record in context. I'm using the following line of code to perform the update: "erManagedObject.setValue([(true, forKey: "aKey"),(false, forKey: "anotherKey")])"
public func updateRecordsForEntityManagedObject(_ entity: String, erManagedObject: [NSManagedObject]){
// Create the Fetch Request
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entity)
let recordCount = erManagedObject.count
print(" Total Records: \(recordCount)")
for i in 1...recordCount {
// I receive the error here
erManagedObject.setValue([(true, forKey: "aKey"),(DateUtilities().getTimestamp(), forKey: "timeStampKey")])
}
Any assistance is greatly appreciated!
Replace
for i in 1...recordCount {
// I receive the error here
erManagedObject.setValue([(true, forKey: "aKey"),(false, forKey: "anotherKey")])
}
With
erManagedObject.forEach {
$0.setValue(true, forKey: "aKey")
$0.setValue(false, forKey: "anotherKey")
}
As you should use the loop item to setValue
not the array itself
let request = NSFetchRequest<NSFetchRequestResult>(entityName:entity)
do {
let result = try context.fetch(request) as! [ModelName]
result.forEach {
$0.someKey = ""
}
// save context here
}
catch {
print(error)
}