J'essaie d'enregistrer un NSMutableArray
appelé queueArray afin qu'il puisse être chargé à nouveau après que l'application a été fermée. J'ai utilisé quelques tutoriels pour me lancer et c'est le code que j'ai trouvé. Le problème semble être que "initWithCoder
" et "encodeWithCoder
" ne sont pas appelés, montrés par aucun NSLog
appels et aucun arrêt aux points d'arrêt. J'ai ajouté le protocole NSCoding
au fichier .h et je sais que queueArray n'est pas nul et il contient MPMediaItem
s. Voici le code que j'utilise pour essayer de sauvegarder et charger le tableau:iOS/iPhone SDK: initWithCoder et encodeWithCoder ne sont pas appelés
-(IBAction)saveQueuePressed {
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [rootPath stringByAppendingPathComponent:@"queueArray.archive"];
//should cause encodeWithCoder to be called
[NSKeyedArchiver archiveRootObject:queueArray toFile:filePath];
}
-(IBAction)loadQueuePressed {
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [rootPath stringByAppendingPathComponent:@"queueArray.archive"];
//should cause initWithCoder to be called
queueArray = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}
-(void)encodeWithCoder:(NSCoder *)coder {
NSLog(@"encodeWithCoder");
[coder encodeObject:queueArray forKey:@"savedQueueArray"];
}
-(id)initWithCoder:(NSCoder *)decoder {
NSLog(@"initWithCoder");
queueArray = [decoder decodeObjectForKey:@"savedQueueArray"];
return self;
}
Toute aide sera grandement appréciée!
Utilisez le "101010 "bouton pour mettre en forme le code. –