这是我第一次尝试序列化数据时,我不能正确地做某事,因为它不起作用。也许是因为我试图序列化的课程是代表或者也许是因为我填充数据的方式或者其他一百万个错误。知道怎么做的人可以看看我的代码并给我一些提示吗?请?
标题文件:
#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
实施文件:
#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
调用代码在此处:
self.template是指向应该是我的序列化类的指针。 也许我不能这样做?
- (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];
}
}
答案 0 :(得分:0)
我注意到在您的init方法中,在解码之前,您正在使用_templateSize
。