0

This is my first try at serializing data and I must not be doing something correctly because it's not working. Perhaps it's because the class I am trying to serialize is a delegate or perhaps it's because of the way I'm populating the data or perhaps a million other things are wrong. Can somebody who knows how to do this take a look at my code and give me some hints? Please?

Header file:

#import <Foundation/Foundation.h>

@protocol IDTemplateDelegate <NSObject>
- (void)saveTemplateForUI;
@end

@interface IDTemplate : NSObject <NSCoding>

@property (nonatomic) const char *templateData;
@property (nonatomic) NSUInteger templateSize;
@property (nonatomic) int templateQuality;
@property (nonatomic) int templateLocation;

@property (nonatomic, assign) id<IDTemplateDelegate> delegate;

@end

Implementation File:

#import "IDTemplate.h"

@implementation IDTemplate
@synthesize delegate;

- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (self) {
        NSData *data = [decoder decodeObjectForKey:@"templateData"];
        [data getBytes:(void *)_templateData length:_templateSize];
        _templateSize = [decoder decodeIntegerForKey:@"templateSize"];
        _templateQuality = [decoder decodeIntForKey:@"templateQuality"];
        _templateLocation = [decoder decodeIntForKey:@"templateLocation"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    NSData *data = [NSData dataWithBytes:_templateData length:_templateSize];
    [encoder encodeObject:data forKey:@"templateData"];
    [encoder encodeInteger:_templateSize forKey:@"templateSize"];
    [encoder encodeInt:_templateQuality forKey:@"templateQuality"];
    [encoder encodeInt:_templateLocation forKey:@"templateLocation"];    
}

- (void)populateTemplate:(IDTemplate *)template
{
    self.templateData = template.templateData;
    self.templateSize = template.templateSize;
    self.templateQuality = template.templateQuality;
    self.templateLocation = template.templateLocation;
}

@end

Calling code is here:

self.template is a pointer to what should be my serialized class. Maybe I can't do it this way?

- (void)generateTemplate:(cv::Mat)src
{
    Template template = *([self.cpp generateTemplate:src]);

    // self.template is a pointer to what should be my serialized class
    // maybe I can't do it this way?
    self.template = [[IDTemplate alloc] init]; 
    self.template.delegate = self;

    [self.template setTemplateData:template.getData()];
    [self.template setTemplateSize:template.getSize()];
    [self.template setTemplateQuality:template.getQuality()];
    [self.template setTemplateLocation:template.getLocation()];

    {// Setting up NSNotification for templateData
        NSMutableDictionary *templateData = [NSMutableDictionary dictionaryWithCapacity:1];
        [templateData setObject:self.template forKey:@"Template"];

        NSNotificationCenter *templateNote = [NSNotificationCenter defaultCenter];
        [templateNote postNotificationName:@"TemplateGenerated" object:nil userInfo:templateData];
    }
}
Patricia
  • 5,019
  • 14
  • 72
  • 152
  • It would help if you actually provided information about the problem. Don't make us read the code and guess. What issue are you having? – rmaddy Nov 19 '13 at 23:56
  • @rmaddy - The problem is that I am getting values back in my calling code but not the serialized values. I put break points in my initWithCoder and encodeWithCoder and it's never getting in there. I am wondering if it's not working because my IDTemplate class is a delegate. I've got a few books and I can look for examples but I've never done this before and I'm not sure exactly how. From what I have read, as long as I'm using NSCoding delegate it should just work. Maybe I'm wrong. Thank you for helping. – Patricia Nov 20 '13 at 00:59
  • 1
    What should be calling `initWithCoder` and `encodeWithCoder:`? Where do you attempt to serialize instances of `IDTemplate`? – rmaddy Nov 20 '13 at 01:04
  • @rmaddy - OK. I will look at this tomorrow. If I can come up with an answer, I will give you the points. Just hang with me, please? OK? When I get an answer, you will be part of the results and I will give you the credit. I promise. :-) Not that you need it, as you have plenty of points, but I like to give the credit to the ones who deserve it. I won't cheat you. I just need advice. Thank you, again. :-) – Patricia Nov 20 '13 at 04:14
  • @rmaddy After sleeping on it and seeing both your's and Mundi's comments I realized that I might be totally wrong about what I am trying to do. I don't really want to actually write out the data to a file. I really want to dump out the char * value to a byte array. I'm losing the value of the char * pointer when I need to be able to get it. So I thought serializing it might be the answer. Now, I'm second guessing that path. I up voted your comment but now I guess I have a new question. Thank you. – Patricia Nov 20 '13 at 18:59

1 Answers1

0

I am noticing that in your init method you are using _templateSize for your before you have decoded it.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • 1
    This is the solution of the problem I think. – Mundi Nov 20 '13 at 12:56
  • @Mundi After sleeping on it and seeing both your's and rmaddy's comments I realized that I might be totally wrong about what I am trying to do. I don't really want to actually write out the data to a file. I really want to dump out the char * value to a byte array. I'm losing the value of the char * pointer when I need to be able to get it. So I thought serializing it might be the answer. Now, I'm second guessing that path. I up voted your comment but now I guess I have a new question. Thank you. – Patricia Nov 20 '13 at 19:01