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];
}
}