开发者

How to store Matrix or Vector data structure in Core Data?

开发者 https://www.devze.com 2023-01-02 13:37 出处:网络
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 i

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 usi开发者_如何学Cng 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?


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


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号