Domanda

I have an NSDictionary containing some objects: an NSSet of UITouches, a UIEvent, and an NSString.

When I try to encode the dictionary to NSData, the string encodes properly. I had an error with the UITouches being encoded, but I found a way to extend the class with some code so that a UITouch can be encoded. However, I still can't encode the UIEvent (which is actually a UITouchesEvent). How can I extend the UIEvent or UIInternalEvent to make them encodable to NSData?

Methods I use for encoding/decoding:

-(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;

}

Error I get:

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

Please let me know if I'm missing any important info regarding debugging. Thanks!

È stato utile?

Soluzione

You have to have UITouch or UITouchesEvent adapt the UICoding protocol. This means it must support these methods:

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

I haven't tried this myself, but it should work if you do this in a class category. The difficulty will be to find out what you need to encode so it can be decoded again into a correct instance, if that's what you need.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top