AnsweredAssumed Answered

Save Array of feature in documentdirectory

Question asked by Stounfr on Nov 10, 2015
Latest reply on Nov 12, 2015 by Stounfr

Hi,

 

i have a NSMutableArray whit Feature and i want to write the NSMutableArray to my NSDocumentDirectory.

 

my code :

 

NSLog(@"ARRAY CARTE SCOLAIRE :%@", arrayCarteScolaire);

        NSArray *arrayCarteSauvegarde = [[NSArray alloc] initWithArray:arrayCarteScolaire];

        NSLog(@"ARRAY SAUVEGARDE :%@", arrayCarteSauvegarde);

        NSString* imageFilename = [NSString stringWithFormat:@"SAUVEGARDE CARTE %@", anneeChoisiTab];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSString* imageFilePathname = [documentsDirectory stringByAppendingPathComponent:imageFilename];

        //on fait la verif pour voir s'il y a les images du bundle dans le DocumentDirectory

        if ([[NSFileManager defaultManager] fileExistsAtPath:imageFilePathname]) {

            NSLog(@"Fichier %@ déjà présent dans le dossier document - Voir pour la modification", imageFilename);

            //on efface et on copie

            NSError *error;

            BOOL success = [[NSFileManager defaultManager] removeItemAtPath:imageFilePathname error:&error];

            if (success) {

                NSLog(@"Le fichier %@ a bien été effacé", imageFilename);

                //UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:@"Congratulation:" message:@"Successfully removed" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];

                //[removeSuccessFulAlert show];

            }

            else

            {

                NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);

            }

            //on copie le fichier

            NSError *error2;

            BOOL yes = [arrayCarteSauvegarde writeToFile:imageFilePathname atomically:YES];

            if (yes) {

                NSLog(@"Fichier %@ créer", imageFilename);

            }

            else {

                NSLog(@"Could not create file -:%@ ",[error2 localizedDescription]);

            }

        }

        else {

            NSLog(@"Fichier %@ non présent dans le dossier document", imageFilename);

            //on va copier le fichier normalement

            NSError *error2;

            BOOL yes = [arrayCarteSauvegarde writeToFile:imageFilePathname atomically:YES];

            //BOOL yes = [NSKeyedArchiver archiveRootObject:arrayCarteSauvegarde toFile:imageFilePathname];

            if (yes) {

                NSLog(@"Fichier %@ créer", imageFilename);

            }

            else {

                NSLog(@"Could not create file -:%@ ",[error2 localizedDescription]);

            }

        }

 

The file is not create in the document directory but no crash.

 

With this code :

 

else {

            NSLog(@"Fichier %@ non présent dans le dossier document", imageFilename);

            //on va copier le fichier normalement

            NSError *error2;

            //BOOL yes = [arrayCarteSauvegarde writeToFile:imageFilePathname atomically:YES];

            BOOL yes = [NSKeyedArchiver archiveRootObject:arrayCarteSauvegarde toFile:imageFilePathname];

            if (yes) {

                NSLog(@"Fichier %@ créer", imageFilename);

            }

            else {

                NSLog(@"Could not create file -:%@ ",[error2 localizedDescription]);

            }

        }

 

The app crash with this error :

 

2015-11-10 13:42:59.243 Ecoles Colleges 06 Antibes[211:11878] -[AGSGDBFeature encodeWithCoder:]: unrecognized selector sent to instance 0x1742a05a0

 

2015-11-10 13:42:59.245 Ecoles Colleges 06 Antibes[211:11878] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AGSGDBFeature encodeWithCoder:]: unrecognized selector sent to instance 0x1742a05a0'

 

Thanks for regards.

Stounfr

Outcomes