I want to be able to store an array in a property called items
. But core data does not store arrays for some reason.
How can I save a collection of data with a varying size in core data?
Please post code or a link to a tutorial.
I want to be able to store an array in a property called items
. But core data does not store arrays for some reason.
How can I save a collection of data with a varying size in core data?
Please post code or a link to a tutorial.
Iterate over your temp array and store it in the following way:
for item in items {
var newItem = NSEntityDescription.insertNewObjectForEntityForName ("Item",
inManagedObjectContext: context) as NSManagedObject
newItem.setValue(item, forKey: "itemname")
}
If I understood your questions wrong, please correct me. Regards, Alex!
You might want to consider creating another Entity, and using Relationship to link both Entities, to have your array of Items.
Although storing an NSArray in Core Data is an architectural discussion, sometimes you might have reasons not to store the objects in the array into a separate entity (over-normalisation, performance issues,..)
If you want to store an array of Foundation types, you can indicate the type of the attribute as Transformable
. Core Data already has NSValueTransformers built in for every Foundation type as of iOS5.
If you have an array of custom NSObject, you might want to write a new NSValueTransformer subclass too handle the persistence of this custom object More on NSValueTransformer can be found here: NSValueTransformer