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