0

主に、UITouch を含む NSDictionary をアーカイブしようとしています。「-[UITouch encodeWithCoder:]: 認識されないセレクターがインスタンスに送信されました」というエラーが表示され続けます。

辞書から UITouch オブジェクトを削除すると、エラーは発生しません。

過去数時間、グーグルで検索するなど、自分でそれを理解しようとしてきましたが、UITouch オブジェクトを NSDictionary に格納する方法をまだ見つけていません。

ここに私が使用している方法があります:

- (void)sendMoveWithTouch:(id)touch andTouchesType:(TouchesType)touchesType {
     MessageMove message;
     message.message.messageType = kMessageTypeMove;
     message.touchesType = touchesType;
     NSData *messageData = [NSData dataWithBytes:&message length:sizeof(MessageMove)];

     NSMutableData *data = [[NSMutableData alloc] init];
     NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

     [archiver encodeObject:@{@"message" : messageData, @"touch": touch} forKey:@"touchesDict"]; //RECEIVE ERROR HERE. If I remove the UITouch object, everything passes correctly

     [archiver finishEncoding];

     [self sendData:data];
}

どんな助けでも大歓迎です。

4

1 に答える 1

1

UITouch は<NSCoding>、自明な/明白なシリアル化された表現 (基本的なデータ型である文字列や配列など) を持たないため、それ自体ではプロトコルに準拠していません。あなたがしなければならないことは、そのクラスを拡張し、どのプロパティをどのような形式でシリアライズするかを決定することによって、このプロトコルに準拠させることです。例えば:

@implementation UITouch (Serializable)

- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:@([self locationInView:self.view].x) forKey:@"locationX"];
    [coder encodeObject:@([self locationInView:self.view].y) forKey:@"locationY"];
}

- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super init]) {
        // actually, I couldn't come up with anything useful here
        // UITouch doesn't have any properties that could be set
        // to a default value in a meaningful way
        // there's a reason UITouch doesn't conform to NSCoding...
        // probably you should redesign your code!
    }
    return self;
}

@end
于 2013-01-15T06:03:00.293 に答える