I'm somewhat new to swift, and I'm not a master when it comes to arrays I'm trying to find a way where I can store an array of NSManagedObjects into core data.
Here's an example:
let array = [["a", "b", "c",],["1", "2", "3",],["apple", "orange", "pear"]]
This is the way I did it:
I have several arrays of NSManagedObjects that I added to a NSMutableArray Then I stored it as transformable data. Like this.
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity_entry = NSEntityDescription.entityForName("EntityForArrays", inManagedObjectContext: managedContext)
let new_value = NSManagedObject(entity: entity_entry!, insertIntoManagedObjectContext: managedContext)
new_value.setValue(array, forKey: "attribute")
So am I going down the right path by using a NSMutableArray or should I try something new?
Also if can store this array, how can I receive it?
Whether it's the right path depends on how you need to use that array. It's convenient to put it all into one transformable attribute, but you can't use predicates to fetch objects based on their array contents.
Saving the array like that should work. To get it back, fetch objects using NSFetchRequest
and then look up the array value using valueForKey()
.