Objective c 序列化/反序列化编码器/解码器不工作

Objective c 序列化/反序列化编码器/解码器不工作,objective-c,serialization,deserialization,initwithcoder,Objective C,Serialization,Deserialization,Initwithcoder,这是我第一次尝试序列化数据,我一定没有做正确的事情,因为它不起作用。可能是因为我试图序列化的类是一个委托,或者可能是因为我填充数据的方式,或者可能是其他一百万件事情都错了。有人谁知道如何做到这一点,看看我的代码,给我一些提示?求你了 头文件: #import <Foundation/Foundation.h> @protocol IDTemplateDelegate <NSObject> - (void)saveTemplateForUI; @end @interfa

这是我第一次尝试序列化数据,我一定没有做正确的事情,因为它不起作用。可能是因为我试图序列化的类是一个委托,或者可能是因为我填充数据的方式,或者可能是其他一百万件事情都错了。有人谁知道如何做到这一点,看看我的代码,给我一些提示?求你了

头文件:

#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
调用代码如下:

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

我注意到,在您的init方法中,在解码之前,您正在使用
\u templateSize
对您的

如果你真的提供了关于这个问题的信息,这会有所帮助。不要让我们读代码然后猜。您遇到了什么问题?@rmaddy-问题是我正在从调用代码中获取值,但没有获取序列化的值。我把断点放在我的initWithCoder和encodeWithCoder中,它永远不会进入那里。我想知道它是否不起作用,因为我的IDTemplate类是一个委托。我有几本书,我可以找一些例子,但我以前从来没有这样做过,我不知道具体怎么做。从我所读到的内容来看,只要我使用nscodingdelegate,它就会正常工作。也许我错了。谢谢您的帮助。应该调用什么
initWithCoder
encodeWithCoder:
?您在哪里尝试序列化
IDTemplate
的实例?@rmaddy-OK。我明天再看这个。如果我能想出一个答案,我会给你分数。请和我在一起好吗?好啊当我得到答案时,你将是结果的一部分,我会给你荣誉。我保证。:-)并不是说你需要它,因为你有很多积分,但我喜欢把这归功于那些应得的人。我不会骗你的。我只是需要建议。再次感谢您。:-)@rmaddy在睡了一觉,看到你和Mundi的评论后,我意识到我可能完全错了。我真的不想将数据写入文件。我真的想把char*值转储到一个字节数组中。当我需要能够获取char*指针时,我正在丢失它的值。所以我想把它连载可能是答案。现在,我在猜测这条路。我对你的评论投了赞成票,但现在我想我有一个新问题。谢谢你。我想这就是问题的解决办法。@Mundi在仔细考虑之后,看到了你和rmaddy的评论,我意识到我可能完全错了。我真的不想将数据写入文件。我真的想把char*值转储到一个字节数组中。当我需要能够获取char*指针时,我正在丢失它的值。所以我想把它连载可能是答案。现在,我在猜测这条路。我对你的评论投了赞成票,但现在我想我有一个新问题。非常感谢。