Question

Je ne suis probablement pas ici quelque chose vois, voilà pourquoi je vous demande de l'aide :) Voici l'affaire que j'ai un tableau NSMutable d'éléments qui remplissent le protocole NSCoding, mais NSKeyedArchiver échoue toujours l'archiver ... voici mon implémentation de l'objet:

@implementation YTVideo
@synthesize URL,thumb,titulo;

#pragma mark NSCoding
#define kTituloKey          @"titulo"
#define kURLKey     @"URL"
#define kThumbKey       @"thumb"

-(id)initWithData:(NSString *)ktitle :(UIImage *)kThumb :(NSURL *)kURL{
self.titulo = ktitle;
self.thumb = kThumb;
self.URL = kURL;

return self;
}

- (void) encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:titulo forKey:kTituloKey];
[encoder encodeObject:URL forKey:kURLKey];
NSData *thumbData = UIImagePNGRepresentation(thumb);
[encoder encodeObject:thumbData forKey:kThumbKey];
}

- (id)initWithCoder:(NSCoder *)decoder {
NSString* ktitulo = [decoder decodeObjectForKey:kTituloKey];
NSURL* kURL = [decoder decodeObjectForKey:kURLKey];
NSData* kThumbdata = [decoder decodeObjectForKey:kThumbKey];
UIImage* kThumb=[UIImage imageWithData:kThumbdata];
return [self initWithData:ktitulo:kThumb:kURL];
}

@end

Au cours de l'exécution du programme que j'ai un tableau NSMutable de ces objets appelés videosArray.

puis, par la suite, je tente:

NSString* path =[NSHomeDirectory() stringByAppendingPathComponent:@"teste.wrapit"];
NSLog(@"PATH =%@",path);
bool teste = [NSKeyedArchiver archiveRootObject:videosArray toFile:path];
NSLog(@"aramzenamento:%@",teste ? @"sucesso!" :@"Nope");
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
NSLog(@"Arquivo armazenado existe?%@",fileExists ?@"Sim":@"Nao");

Et je reçois toujours un échec sur mes chèques booléennes ... Toutes les idées où je suis tout à fait tort ?? Merci !!

Était-ce utile?

La solution

Le problème que vous rencontrez n'a rien à voir avec NSKeyedArchiver. Par le regard de celui-ci, vous essayez d'archiver votre objet au niveau de la racine de votre bac à sable (le répertoire retourné par NSHomeDirectory()). Essayez de remplacer la première ligne du deuxième bloc de code avec

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
path = [path stringByAppendingPathComponent:@"teste.wrapit"];

Un autre (peut-être plus propre) façon d'obtenir le chemin de votre dossier Documents est d'utiliser la fonction C NSSearchPathForDirectoriesInDomains, qui retourne un tableau de chemins qui correspondent au premier argument:

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"teste.wrapit"];

Il est pointant vers une valeur que, sur iOS, la fonction NSSearchPathForDirectoriesInDomain est garantie pour retourner un NSArray avec un seul élément lorsque vous utilisez une constante intégrée (comme NSDocumentDirectory) pour le premier argument, de sorte que vous pouvez utiliser en toute sécurité objectAtIndex:0 sur la matrice.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top