How to save a NSArray/NSMutableArray in Core Data
Any class you have which conforms to the NSCoding protocol can be serialized and archived in this way. NSArray/NSMutableArray already conform this protocol. Its important to say that all of objects of the array must conform too.
If you have in your model an entity with a property of type 'Binary Data', you can store in it the array data by doing:
[...]
NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:TheArray];
myEntity.arrayProperty = arrayData;
[self saveContext]; //Self if we are in the model class
[...]
Then you can retrieve all the original array info by doing the opposite operation:
[...]
NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:anEntity.arrayProperty];
[...]
Written by Juan Fernández Sagasti
Related protips
8 Responses
Thanks !! This post saved my life!
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/32850/u1b6bd8ad071161fb5bd43535867f4590.jpg)
I'm glad to hear that! :)
Why would you do that? Create new model for the NSArray entries?
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/73235/0_ZPIPybzmZvmCO8Las-M8yXkm4BEfyQLaVt02yXkYFAY0_G-mqvRx-krK9sor0TXGMKdSP_V4d_3o.jpeg)
Awesome..
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/80295/333a334c7ef9850538f348f9868e1ef1.jpeg)
Thanks dude, this was really helpful.
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/93800/0_LNotMmq2RG7ZYFwi51Z3M2GSR6ZBYkwibPODMD-gXiuQThy_WtVYcSQPze4Wj_EfXBHunE8c6Wh8.jpeg)
thanks a lot..precise and great.. saved lot of my time
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/32850/u1b6bd8ad071161fb5bd43535867f4590.jpg)
Great! Thanks for your contribution simonschellaert :)
Yes, Its works for storing array of NSObject in transformable field.
but Is is possible to store array of NSManagedObject in transformable field?
i tried it but its giving an error.
please help me.
Thanks in advance.