1

我有一个包含一些对象的NSDictionary:一个UITouches,一个UIEvent和一个NSString的NSSet。将UITouchesEvent作为NSData编码时出错

当我尝试将字典编码为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 

请让我知道如果我没有关于调试任何重要信息。谢谢!

回答

1

您必须有UITouchUITouchesEvent修改UICoding协议。这意味着它必须支持这些方法:

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

我没有尝试这样做自己,但如果你这样做的一类类它应该工作。难点在于找出需要编码的内容,以便它可以再次解码为正确的实例,如果这是您需要的。

+0

谢谢,但正如我所说,我已经有UITouch编码工作。我试图找出如何与UITouchesEvent做到这一点。 – rsmoz 2013-02-09 05:02:51

+0

我的不好,当然你必须为'UITouchesEvent'类做这个。但我不认为这会很容易。 – Pascal 2013-02-09 20:29:26