繁体   English   中英

将UITouchesEvent编码为NSData时出错

[英]Error when encoding UITouchesEvent as NSData

我有一个NSDictionary,其中包含一些对象:UITouches的NSSet,UIEvent和NSString。

当我尝试将字典编码为NSData时,字符串会正确编码。 我对UITouches进行编码时出错,但是我找到了一种使用一些代码扩展类的方法,以便可以对UITouch进行编码。 但是,我仍然无法对UIEvent进行编码(实际上是UITouchesEvent)。 如何扩展UIEvent或UIInternalEvent以使它们可编码为NSData?

我用于编码/解码的方法:

-(NSString *)stringFromDictionary:(NSDictionary *)dict{
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:dict forKey:@"dictKey"];
    [archiver finishEncoding];

    return [Base64 encode:data];
}

-(NSDictionary *)dictionaryFromString:(NSString *)string{
    NSData *data = [[NSMutableData alloc] initWithData:[Base64 decode:string]];
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    NSDictionary *myDictionary = [unarchiver decodeObjectForKey:@"dictKey"];
    [unarchiver finishDecoding];
    return myDictionary;

}

错误我得到:

-[UITouchesEvent encodeWithCoder:]: unrecognized selector sent to instance

如果我缺少有关调试的任何重要信息,请告诉我。 谢谢!

您必须让UITouchUITouchesEvent适应UICoding协议。 这意味着它必须支持以下方法:

- (id)initWithCoder:(NSCoder *)decoder;
- (void)encodeWithCoder:(NSCoder *)encoder;

我自己还没有尝试过,但是如果您在班级类别中这样做,它应该可以工作。 困难将是找出您需要编码的内容,以便可以将其再次解码为正确的实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM