I have been trying to implement the answers given at is it possible to save NSMutableArray or NSDictionary data as file in iOS? and they have not been working for me. Will someone please inform me of what I am doing wrong? My NSArray is of a custom object, say Cat
. Cat itself is constituted of strings and numbers. Here is my code
-(void)saveArrayToFile:(NSArray *)response;
{
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDirectory = [paths objectAtIndex:0];
// NSString *filePath = [documentsDirectory stringByAppendingPathComponent:PERSISTENT_FILENAME];
//
// [response writeToFile:filePath atomically:YES];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
if ([paths count] > 0)
{
// Path to save array data
NSString *arrayPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"array.out"];
// Write array
[response writeToFile:arrayPath atomically:YES];
}else NSLog(@"NO paths");
}
-(NSArray *)getArrayFromFile
{
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDirectory = [paths objectAtIndex:0];
// NSString *filePath = [documentsDirectory stringByAppendingPathComponent:PERSISTENT_FILENAME];
// return [NSArray arrayWithContentsOfFile:filePath];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
if ([paths count] > 0)
{
// Path to save array data
NSString *arrayPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"array.out"];
NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:arrayPath];
return arrayFromFile;
}else NSLog(@"No file to retrieve");
return nil;
}
First I tried the commented code. Then I tried the other. The problem so far is that the file is not being saved.