¿Cómo save Array en CoreData?

Necesito save mi matriz en Core Data.

let array = [8, 17.7, 18, 21, 0, 0, 34] 

Los valores dentro de esa matriz y la cantidad de valores son variables.

1. ¿Qué declaro dentro de mi class NSManagedObject?

 class PBOStatistics: NSManagedObject, Equatable { @NSManaged var date: NSDate @NSManaged var average: NSNumber @NSManaged var historicAverage: NSNumber @NSManaged var total: NSNumber @NSManaged var historicTotal: NSNumber @NSManaged var ordersCount: NSNumber @NSManaged var historicOrdersCount: NSNumber @NSManaged var values: [Double] //is it ok? @NSManaged var location: PBOLocation } 

2. ¿Qué declaro dentro de mi .xcdatamodel?

introduzca la descripción de la imagen aquí

3. ¿Cómo guardo esto en mi Entidad? (Yo uso MagicalRecord)

 let statistics = (PBOStatistics.MR_createInContext(context) as! PBOStatistics) statistics.values = [8, 17.7, 18, 21, 0, 0, 34] //is it enough? 

Ok, hice algunas investigaciones y testings. Usando el tipo Transformable , la solución es simple:

1. ¿Qué declaro dentro de mi class NSManagedObject?

 @NSManaged var values: [NSNumber] //[Double] also works 

2. ¿Qué declaro dentro de mi .xcdatamodel?

Transformable tipo de datos.

3. ¿Cómo guardo esto en mi Entidad?

 statistics!.values = [23, 45, 567.8, 123, 0, 0] //just this 

"Puede almacenar un NSArray o un NSDictionary como un atributo transformable. Esto utilizará la NSCoding para serializar la matriz o el dictionary a un atributo NSData (y deserializarlo apropiadamente al acceder) "- Fuente

O si quiere declararlo como Datos binarys , lea este artículo simple :

Convertir matriz a NSData

 let appDelegate = UIApplication.shanetworkingApplication().delegate as! AppDelegate let managedContext = appDelegate.managedObjectContext let entity = NSEntityDescription.entityForName("Device", inManagedObjectContext:managedContext) let device = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext) let data = NSKeyedArchiver.archivedDataWithRootObject(Array) device.setValue(data, forKey: "dataOfArray") do { try managedContext.save() devices.append(device) } catch let error as NSError { print("Could not save \(error), \(error.userInfo)") } 

Seleccionar datos binarios

Convertir NSData a Array

 let appDelegate = UIApplication.shanetworkingApplication().delegate as! AppDelegate let managedContext = appDelegate.managedObjectContext let fetchRequest = NSFetchRequest(entityName: "Device") do { let results = try managedContext.executeFetchRequest(fetchRequest) if results.count != 0 { for result in results { let data = result.valueForKey("dataOfArray") as! NSData let unarchiveObject = NSKeyedUnarchiver.unarchiveObjectWithData(data) let arrayObject = unarchiveObject as AnyObject! as! [[String: String]] Array = arrayObject } } } catch let error as NSError { print("Could not fetch \(error), \(error.userInfo)") } 

Por ejemplo: https://github.com/kkvinokk/Event-Tracker

Swift 2.3

 import UIKit import CoreData @objc(Merchant_Status) class Merchant_Status: NSManagedObject { @NSManaged var data: [AnyObject] @NSManaged var uniqueKey: String } // MARK: - // MARK: - MERCHANT STATUS extension CoreDataHelper{ class func saveMerchantStatus(data :[AnyObject]) { let uniqueKey = MERCHENT_INFO.MERCHANT_ID let context: NSManagedObjectContext = CoreDataHelper.managedObjectContext() let request = NSFetchRequest(entityName: "Merchant_Status") request.pnetworkingicate = NSPnetworkingicate(format: "uniqueKey = %@", uniqueKey) do { let fetchedEntities = try CoreDataHelper.managedObjectContext().executeFetchRequest(request) if fetchedEntities.count != 0{ let managedObject = fetchedEntities[0]as! NSManagedObject managedObject.setValue(data, forKey: ConetworkingataConstant.kDATA) do { try context.save() } catch { print(error) } }else{ let entityDescription = NSEntityDescription.entityForName("Merchant_Status", inManagedObjectContext:CoreDataHelper.managedObjectContext()) let context = NSManagedObject(entity: entityDescription!, insertIntoManagedObjectContext:CoreDataHelper.managedObjectContext()) context.setValue(data, forKey: ConetworkingataConstant.kDATA) context.setValue(uniqueKey, forKey: ConetworkingataConstant.kUNIQUE_KEY) //unique value do { try context.managedObjectContext?.save() } catch { print(error) } } } catch { print(error) } } class func getMerchantStatus() -> [String:AnyObject]{ let fetchRequest = NSFetchRequest() let entityDescription = NSEntityDescription.entityForName("Merchant_Status", inManagedObjectContext: CoreDataHelper.managedObjectContext()) fetchRequest.entity = entityDescription var parameters:[String:AnyObject] = [String: AnyObject]() do { let result = try CoreDataHelper.managedObjectContext().executeFetchRequest(fetchRequest) if (result.count > 0) { let person = result[0] as! NSManagedObject let deposits_Amount:[AnyObject] = person.valueForKey(ConetworkingataConstant.kDATA)! as! [AnyObject] parameters = deposits_Amount[0] as! [String:AnyObject] } } catch { let fetchError = error as NSError print(fetchError) } return parameters } } 

introduzca la descripción de la imagen aquí