I’m working on a project that has C code embedded within Objective-C code. The C code produces some void * pointers that I would like to pass between Objective-C methods, so I’d like to wrap them in Objective-C objects to make an NSSet or something to that effect.
I have looked into NSData, which seems to accept arbitrary data, but wants to know the length of the memory chunk, that I don’t have.
Any help is appreciated.
The
NSValueclass is usually used for this task:and
Note, though, that the pointer given will not receive any special treatment with respect to memory management. You (and you alone) are responsible for making sure, that the memory it points to remains valid as long as pointers to that memory region exist and are reachable in some part of your program.
Note, though, that using such a value with
NSSetor as key in aNSDictionarymight or might not yield the intended effect:NSDatais essentially a byte buffer. It actually represents the content of the memory in question. ComparingNSDatainstances for equality compares at byte level. This is one of the reasons,NSDataneeds to know about the length of the memory region in question.NSValuewith a pointer value is an entirely different beast. Here, that actual (numeric) pointer value is the essential thing. No consideration is given (when comparing twoNSValueinstances) to the actual content at the address.