Thomas
Thomas

Reputation: 324

How to clear the contents of all the attributes of an entity?

I have data recorded in the attributes of a entities in coredata. I would like to erase the data saved in each attribute of an entity. Please is there a way to do it and if so, how?

Thank you

Upvotes: 2

Views: 144

Answers (1)

Tziki
Tziki

Reputation: 330

Get the NSEntityDescription of that object (that's the entity property of the NSManagedObject). And then you can simply iterate on the names (via the attributesByName property) and set all values to nil

for (name, attributes) in entity.attributesByName { setValue(nil, forKey: name) }

You can do the same for the relationships of that object (via the relationshipsByName property)

for (name, relationship) in entity.relationshipsByName { setValue(nil, forKey: name) }

With this methods you can also exclude specific properties (if there are some you don't want to clear).

Upvotes: 3

Related Questions