0

这是我第一次尝试序列化数据,我不能正确地做某事,因为它不起作用。也许是因为我试图序列化的类是一个委托,或者是因为我填充数据的方式,或者可能有一百万个其他事情是错误的。知道如何执行此操作的人可以查看我的代码并给我一些提示吗?请?

头文件:

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

1 回答 1

0

我注意到在您的 init 方法中_templateSize,您在解码之前使用它。

于 2013-11-20T00:56:00.160 回答