I need to save my array to Core Data.
let array = [8, 17.7, 18, 21, 0, 0, 34]
The values inside that array, and the number of values are variable.
1. What do I declare inside my NSManagedObject class?
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. What do I declare inside my .xcdatamodel?
3. How do I save this in my Entity? (I use MagicalRecord)
let statistics = (PBOStatistics.MR_createInContext(context) as! PBOStatistics)
statistics.values = [8, 17.7, 18, 21, 0, 0, 34] //is it enough?
Swift 3 As we don't have the implementation files anymore as of Swift 3, what we have to do is going to the xcdatamodeld file, select the entity and the desired attribute (in this example it is called values). Set it as transformable and its custom class to
[Double]
. Now use it as a normal array.Make entity attribute type as "Binary Data"
Retrive original array as:
That's all.
If keeping it simple and store an array as a string
Try this:
For other data types respectively:
Ok, I made some research and testing. Using Transformable type, solution is simple:
1. What do I declare inside my NSManagedObject class?
2. What do I declare inside my .xcdatamodel?
Transformable
data type.3. How do I save this in my Entity?
Or If you want to declare it as Binary Data then read this simple article:
Convert Array to NSData
Convert NSData to Array
For Example : https://github.com/kkvinokk/Event-Tracker