I have limited experience working with the Core Foundation types & collections, so apologies if this is obvious.
I’m using the CFBitVector type to store some bit sequences, and I need to store it in a binary data format (so that it can be added to a Core Data store). The most sensible thing seems to be to store this in a CFData type, which can be toll-free bridged with an NSData and added to the store, but I am unsure of how to do this.
Can anybody help me out with a simple example of storing CFTypes in CF/NSData?
Edit:
Is this even the right approach? Should I try converting the CFBitVector into a series of ints which can then be stored in the data model? Or perhaps a transformable attribute?
The way I ended up doing this was to roll my own attribute transformer in order to convert a
CFBitVectorRefinto anNSDatainstance. The benefit of this is that I can really cram the bit array tightly into a block of binary data, as in my case I really need to keep the storage size to a minimum.Below is the implementation of my
CFBitVectorTransformerclass. It essentially reads each bit and packs them intounsigned chars (“segments” in the code below), which are then appended to a mutableNSDatabuffer. The code would work with types larger thanunsigned chars, however I wanted the smallest chunks possible in order to really minimise the size of the resulting data.This nicely abstracts the conversion of data, allowing you to just set the
CFBitVectorRefas an attribute in the data model, and should be plenty fast enough for most purposes.I hope this helps somebody else in a similar situation.