0

I do have a simple Integer Array that I want to save/load to CoreData.

Do you have example of the best way to do both of the methods ?

Thank you!

Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71
Kevin Sabbe
  • 1,412
  • 16
  • 24
  • Possible duplicate : http://stackoverflow.com/a/36385769/3400991 – Shobhakar Tiwari Dec 09 '16 at 12:41
  • I think it's a bit different – Kevin Sabbe Dec 09 '16 at 12:50
  • A simple integer? What does it represent? Because if it has a "real" meaning, you could create an entity for it, and use a one to many relationship, else, your could also save the int, as data (using for example JSON representation, NSCoding), as String (componentsJoinedByString), etc. – Larme Dec 09 '16 at 12:54
  • it's an Integer Array that represent "choices history" from QuestionsForm something like that [2, 1, 0, 3, 2, 1, 4, 2, 3, 4...] – Kevin Sabbe Dec 09 '16 at 13:07
  • 2
    *Fastest, but not cleanest way*: Declaring CoreData's field as `Transformable` with `[Int]` as `Custom Class` (assuming you set code generation to Swift for the model) will work. CoreData will archive/dearchive data behind offscreen. – user28434'mstep Dec 09 '16 at 14:01
  • I did an extension of Array to String and vice versa, this way I store my array as String in CoreData – Kevin Sabbe Dec 09 '16 at 15:16

1 Answers1

3

Create an attribute with type binaryData in your entity. And convert your array to data using

    NSKeyedArchiver.archivedDataWithRootObject(AnyObject)

And convert the array from data using

    NSKeyedUnarchiver.unarchiveObjectWithData(NSData)

By this way you can store any object in core data.

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70