0

I am using the answer to this question to save an NSArray to a file. I can't retrieve the array though. I am getting 0x00000000 for my retrieved boundaries array when I do the following:

to save:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"boundaries"];

[array writeToFile:filePath atomically:YES];

to retrieve:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"boundaries"];

NSArray *boundaries = [NSArray arrayWithContentsOfFile:filePath];

Is something wrong with this? The array is full of correct data when I save it.

I tried saving [myArray description] and I saved it and retrieved it okay, but the data is obviously different than what I want to have. I'm saving 8 doubles in the array.

Community
  • 1
  • 1
SirRupertIII
  • 12,324
  • 20
  • 72
  • 121
  • 1
    Go to the app's sandbox and check whether you are able to save file correctly or not. – Puneet Sharma Aug 27 '13 at 06:48
  • How do you know that the file has been written successfully? You're not checking the return value from the write. Also, as enricmacias says in his answer, you are writing a variable called `array` Are you sure it's got anything in it? – JeremyP Aug 27 '13 at 08:48
  • Did I say I knew it was written correctly? I just said that the array is filled with 8 doubles... I know because I set a break point.. and did `po array' and it gave me 8 doubles. – SirRupertIII Aug 27 '13 at 08:55

2 Answers2

2
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"boundaries"];

[array writeToFile:filePath atomically:YES];

Where is this 'array' variable coming from?

Anyway you won't we able to save doubles directly into a NSArray. You need to save those doubles as NSNumber. Like this:

[[NSNumber alloc] initWithDouble:doubleNumber];
enricmacias
  • 142
  • 2
  • 9
  • I don't want to show the entire method that shows how I retrieve data from the database and set the array with that data. That's why I just put in "array" and said that the array is filled with 8 doubles... If you want to see that I sure can put it up there. – SirRupertIII Aug 27 '13 at 08:55
  • It's ok. Just make sure the information in that array is correct. This may help: http://stackoverflow.com/questions/3010363/nsarray-writetofile-fails It seems NSNumber are not valid values for writeToFile: Try saving NSString instead of NSNumber – enricmacias Aug 27 '13 at 09:57
1

Get the data of the array using

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array]; 

and then save it in your documents directory. To Retrieve it back,

NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];

This should work.

Mani Kandan
  • 629
  • 4
  • 12