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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.