Store NSArray In Core Data Sample Code?

Stunner picture Stunner · Dec 28, 2010 · Viewed 32.1k times · Source

I have been searching for some sample code on how to store an NSArray in Core Data for awhile now, but haven't had any luck. Would anyone mind pointing me to some tutorial or example, or better yet write a simple sample as an answer to this question? I have read this but it doesn't show an example of how to go about implementing a transformable attribute that is an NSArray. Thanks in advance!

Answer

Resh32 picture Resh32 · Nov 13, 2012

If you really need to do it, then encode as data. I simply created a new filed called receive as NSData (Binary data).

Then in the NSManagedObject implementation:

-(void)setReceiveList:(NSArray*)list{
     self.receive = [NSKeyedArchiver archivedDataWithRootObject:list];
}

-(NSArray*)getReceiveList{
    return [NSKeyedUnarchiver unarchiveObjectWithData:self.receive];
}