views:

127

answers:

2

I am building an iPhone application that needs to store some sort of matrix or vector data in core data. For some unknown reason, the iPhone SDK does not include any kind of matrix data structure in its foundation classes, so I built my own data structure which uses an NSMutableArray of NSMutableArrays to store the data. So far so good.

I'd like to store this data structure in my core data store, and I've read about using transformable attributes to store non-standard structures. When I try to store the data structure, I get the following error: [FCMatrix encodeWithCoder:]: unrecognized selector sent to instance The code that I am running is as follows, essentially just setting the attribute in my entity:

[collection setMatrix:matrix];

My assumption at this point is that since matrix is a transformable attribute, and my FCMatrix class is a custom object (subclassed from NSObject), I need to implement the method encodeWithCoder in my data structure class so that Core Data can properly encode, store, and then decode the data to recreate my data structure. Can anyone explain the best way to do this?

Another idea - I'd like to know what you think - is to rebuilt my FCMatrix class to be subclassed from NSMutableArray. If NSMutableArray could be transformable, then I would be set.

Alternately, I am convinced that this is way too much work and that there has to be an easier way to do this. Does anyone have any experience storing matrix data in core data?

A: 

Maybe look into converting the arrays into bytes, which can be stored in an NSData object and placed into a binary attribute.

Alex Reynolds
A: 

I have come up with a solution to my own problem:

  • Core Data can store NSMutableArray objects in core data as transformable attributes.
  • If I create a custom Matrix class, e.g. "FCMatrix", then core data will not know how to store it as a transformable object.
  • Therefore, I can create my Matrix class as essentially an entirely static class. Then, I can just pass a NSMutableArray object to the functions and operate on them using my old logic.
Jason
I would suggest translating it into a readable, consumable form before storing it. For example, you could store it as a JSON data structure inside of a string in Core Data. Binary can be fickle and if it goes wrong there is no way to make it right.
Marcus S. Zarra