0

I'm trying to write an NSArray of NSDictionaries to the documents folder. I'm using

BOOL fileWritten = [favoritesArray writeToFile:fullFileName atomically:NO];
    if (fileWritten == NO) {
        NSLog(@"Writing favoritesFileFailed");
    }

The above keeps returning NO. Any idea why this wouldn't be writing? What's really strange is that this same code was working a few days ago. I read something stating that writeToFile can't handle complex objects. Would NSDictionary be an object writeToFile can't save?

Monolo
  • 18,205
  • 17
  • 69
  • 103
Sam
  • 598
  • 4
  • 16
  • What your dictionaries create? What keys dictionaries use? Is file path correct? – Vladimir Jun 19 '12 at 14:18
  • What objects do the NSDictionary objects contain? I believe it's perfectly fine to archive Arrays and Dictionaries, but if they contain a non-arhivable object then that is where the problem lies. – Cocoadelica Jun 19 '12 at 14:18

4 Answers4

0

Yeh if you have got custom objects i would do some NSCoding magic and then you can use [NSKeyedArchiver archivedDataWithRootObject:favoritesArray]

So you need to make sure each custom class within that array conforms to NSCoding Reference and that should work.

Martin
  • 747
  • 6
  • 11
0

Writing the NSDictionary itself to a file is not a problem, but the objects it holds may be.

By using the simple property list-related I/O methods, you can write out objects of classes NSArray, NSDictionary, NSData, NSString, NSNumber, NSDate, and any combination thereof (e.g., arrays of dictionaries).

Also, it won't overwrite an existing file, so have you checked that?

Monolo
  • 18,205
  • 17
  • 69
  • 103
0

First check that the fullFileName actually points to the documents directory.

Then remember that [NSArray: writeToFile:atomically:] can only write datatypes that can be saved in a plist file: NSString, NSData, NSDictionary and NSArray - and those will be validated recursively!

Peter Sarnowski
  • 11,900
  • 5
  • 36
  • 33
0

Try saving a simple array with a simple object. Perhaps one that only has a number, and see if the problem persists. If it doesn't then the objects in the array are probably something to consider saving somewhere else.

Have you tried saving it to NSUserDefaults? Why not try that if all else fails?

Fernando Cervantes
  • 2,962
  • 2
  • 23
  • 34