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!

6
  • Possible duplicate : stackoverflow.com/a/36385769/3400991 Commented Dec 9, 2016 at 12:41
  • I think it's a bit different Commented Dec 9, 2016 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. Commented Dec 9, 2016 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...] Commented Dec 9, 2016 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. Commented Dec 9, 2016 at 14:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Been searching and searching for a clean way to save an array. This works really well. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.