IPB

Bienvenue invité ( Connexion | Inscription )

 
Reply to this topicStart new topic
> Archiver une NSValue avec NSKeyedArchiver [Résolu]
Options
schlum
posté 15 Jun 2006, 08:14
Message #1


Terminaltor
Moderating Machine
*****

Groupe : Admin
Messages : 24 449
Inscrit : 25 Oct 2002
Lieu : Jeumont (59)
Membre no 4 319



Je me suis cassé la tête pas mal de temps sur ce problème (apparemment connu) de l'impossibilité d'encoder une NSValue dans un NSKeyedArchiver...

On tombe sur une exception :
CODE
exception: *** -[NSKeyedArchiver encodeValueOfObjCType:at:]: this archiver cannot encode structs


On trouve ce problème à plusieurs endroits sur internet, mais jamais de solution concrète :
http://www.cocoabuilder.com/archive/messag...003/10/27/82344
http://lists.apple.com/archives/cocoa-dev/...r/msg02066.html
http://lists.apple.com/archives/Cocoa-dev/...n/msg00882.html

D'un autre côté, NSArchiver (deprecated) est capable d'enregistrer ce genre de données et même des tableaux multi-dimensionnels et ce de manière Endian-safe (pourquoi Apple n'a pas repris ce système dans NSKeyedArchiver... Mystère unsure.gif )

Je vous propose donc une solution potable... Si vous avez une meilleure solution ou des idées pour améliorer celle-ci, je suis preneur wink.gif

1 - Créer une catégorie de NSKeyedArchiver

CODE
//
//  MyKeyedArchiver.h
//  SudokuX
//
//  Created by schlum on 14/06/06.
//  Copyright 2006 schlum Corporation. All rights reserved.
//

#import <Cocoa/Cocoa.h>



@interface NSKeyedArchiver (MyKeyedArchiver)



- (void)encodeValueOfObjCType:(const char *)valueType at:(const void *)address;



@end


CODE
//
//  MyKeyedArchiver.m
//  SudokuX
//
//  Created by schlum on 14/06/06.
//  Copyright 2006 schlum Corporation. All rights reserved.
//

#import "MyKeyedArchiver.h"



@implementation NSKeyedArchiver (MyKeyedArchiver)



- (void)encodeValueOfObjCType:(const char *)valueType at:(const void *)address
{
    NSMutableData *datas = [NSMutableData data];
    NSArchiver *arch = [[NSArchiver alloc] initForWritingWithMutableData:datas];
    [arch encodeValueOfObjCType:valueType
                             at:address];
    [self encodeObject:[NSData dataWithData:datas]];
    [arch release];
}



@end


2 - Créer une catégorie de NSKeyedUnarchiver

CODE
//
//  MyKeyedUnarchiver.h
//  SudokuX
//
//  Created by schlum on 14/06/06.
//  Copyright 2006 schlum Corporation. All rights reserved.
//

#import <Cocoa/Cocoa.h>


@interface NSKeyedUnarchiver (MyKeyedUnarchiver)



- (void)decodeValueOfObjCType:(const char *)valueType at:(void *)data;



@end


CODE
//
//  MyKeyedUnarchiver.m
//  SudokuX
//
//  Created by schlum on 14/06/06.
//  Copyright 2006 schlum Corporation. All rights reserved.
//

#import "MyKeyedUnarchiver.h"



@implementation NSKeyedUnarchiver (MyKeyedUnarchiver)



- (void)decodeValueOfObjCType:(const char *)valueType at:(void *)data
{
    NSData *datas = [self decodeObject];
    NSUnarchiver *unarch = [[NSUnarchiver alloc] initForReadingWithData:datas];
    [unarch decodeValueOfObjCType:valueType
                               at:data];
    [unarch release];
}



@end


--------------------
          I think therefore I Mac          
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 utilisateur(s) sur ce sujet (1 invité(s) et 0 utilisateur(s) anonyme(s))
0 membre(s) :

 



Nous sommes le : 27th April 2024 - 00:57