I'm trying to pass key value pairs and have the attributes updated through a loop.
func update(storableClass : NSManagedObject.Type, id: ID, fields: [String : Any]) throws {
// retreive manaagedObject
for (key, value) in fields {
manaagedObject.setValue(value, forKey: key)
}
}
Apparently the setValue(_:forKey:)
will throw an exception if the key doesn't exist.
From what I've learned you're not to catch an Objective-C exception in Swift.
Is there any safe way to update core data properties through a dictionary?
I know I can have a function like below:
func update(storableClass : ManagedObject.Type, id: ID, closure: (ManagedObject) -> ()) throws {}
and then call it as such:
update(storableClass: UserEntity.self, id: "123123", closure: { userEntity in
userEntity.name = "new name"
})
I like to have both options...and any other safe/swifty option...