0
votes

This is the first time for me to try to serialize/deserialize an object. One member of the object is a type of const char *.

----------------------------------------Added info here----------------------------------

This const char * is a byte array that represents an image. Because of image recognition requirements, I really need to get the values to the UI and back from the UI and store it as a byte array. I hope this clears up some confusion.

-------------------------------------------Ended here-------------------------------------

I am completely baffled by the options I have for both decoding and encoding. I cannot make sense out of which option to use and the only one that makes sense is asking for an at:(const void *)array. What goes there?

- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (self) {

Below is the line that I cannot figure out. I have the first two parameters alright but what is the third one? The parameter for at: is an array. But I don't have an array. Am I supposed to be decoding these to an array? If so, then I need to initialize an array to put the chars into?

        _templateData = [decoder decodeArrayOfObjCType:_templateData count:_templateSize at:<#(void *)#>]; //const char *templateData; 

After that it seems OK:

        _templateSize = [decoder decodeIntegerForKey:@"templateSize"];       //NSUInteger templateSize;
        _templateQuality = [decoder decodeIntForKey:@"templateQuality"];     //int templateQuality;
        _templateLocation = [decoder decodeIntForKey:@"templateLocation"];   //int templateLocation;
    }
    return self;
}

And then there is encoding it.

- (void)encodeWithCoder:(NSCoder *)coder
{

Same problem here. I don't have an array. Do I need to initialize an array to put the chars into? Is that how this line is supposed to work?

    [coder encodeArrayOfObjCType:_templateData count:_templateSize at:<#(const void *)#>]

The rest appears OK, again:

    [coder encodeInteger:_templateSize forKey:@"templateSize"];
    [coder encodeInt:_templateQuality forKey:@"templateQuality"];
    [coder encodeInt:_templateLocation forKey:@"templateLocation"];

}

Has anyone out there successfully encoded and decoded a type that is a const char *?

Thank you for your help.

1
Turn it into an NSString - Charlie Burns
Why is this tagged C++ and C? - Taylor Brandstetter
Because I have Objective-C talking to Objective-C++ and then to C++. - Patricia
@CharlieBurns - My const char array is really an image. I don't know what kind of encoding to use for that conversion. - Patricia
Ah, use NSData as shown below then. - Charlie Burns

1 Answers

2
votes

It sounds like the best solution is to turn your array of data into an NSData instance, and encode that. It's pretty straightforward:

NSData *data = [NSData dataWithBytesNoCopy:_templateData length:lengthOfTemplateData freeWhenDone:NO];
[coder encodeObject:data forKey:@"templateData"];

Then, for decoding:

NSData *data = [decoder decodeObjectForKey:@"templateData"];
[data getBytes:_templateData length:sizeOfTemplateDataBuffer]; // Assumes _templateData has been allocated to hold sizeOfTemplateDataBuffer bytes.