21

I am trying to save an array, which has some dictionaries inside, to a plist file but it fails. I don't get any errors. I do exactly the same few lines above in the code just with another array and that works.. I can't figure out why it does not save the file.

This is where I save the file: (see some debugger output below)

// When built parse through dictionary and save to file
    for ( NSString *keys in [dicByCountry allKeys] )
    {
        NSArray *arrr = [[NSArray alloc] initWithArray:[dicByCountry objectForKey:keys]];
        NSString *fname = [self filePath:[NSString stringWithFormat:@"regions.cid%@.plist",keys]];
        if (![arrr writeToFile:fname atomically:YES])
            NSLog(@"Could not write file regions.cid%@.plist",keys);
    }

Here some GDB Output

(gdb) po fname
/Users/chris/Library/Application Support/iPhone Simulator/4.0/Applications/44A9FF9E-5715-4BF0-9BE2-525883281420/Documents/regions.cid0.plist


(gdb) po arrr
<__NSArrayI 0x8022b30>(
{
    countryID = "<null>";
    region = "?\U00e2vora";
    regionID = 16;
},
{
    countryID = "<null>";
    region = Vicenza;
    regionID = 14;
},
{
    countryID = "<null>";
    region = Wales;
    regionID = 23;
}
)
Chris
  • 3,581
  • 8
  • 30
  • 51

2 Answers2

37

If you read the documentation closely, writeToFile:atomically: expects the array to contain only objects which can be written into a plist file.

Only objects of type:

  • NSString
  • NSData
  • NSDate
  • NSNumber
  • NSArray
  • NSDictionary

are permitted. If you have arrays or dictionaries within the array you're saving, their values will be examined by the same criteria.

This is somewhat more restrictive than what's usually allowed in NSArrays. In particular, the value [NSNull null] is not acceptable.

Eric
  • 16,003
  • 15
  • 87
  • 139
warrenm
  • 31,094
  • 6
  • 92
  • 116
  • 1
    What about NSNumber? What about nil? – M. Ryan Jun 21 '10 at 20:04
  • Arrays can't contain `nil` and the documentation doesn't indicate that `NSNumber` s are valid as property list values. – warrenm Jun 22 '10 at 01:01
  • So the array can't contain any custom object? – Frost Jun 23 '10 at 17:06
  • It can contain a custom object that's been serialized to an NSData. Again, a precondition of the writeToFile method is that the array be exactly isomorphic to a property list file, which restricts the contained data types quite severely. – warrenm Jun 23 '10 at 18:40
27

I convert NSArray or NSDictionary to NSData before serializing. Following is a category on nsarray for serializing and deserializing. This comfortableby handles some data being nsnull

@implementation NSArray(Plist)

-(BOOL)writeToPlistFile:(NSString*)filename{
    NSData * data = [NSKeyedArchiver archivedDataWithRootObject:self];
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentsDirectory = [paths objectAtIndex:0];
    NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
    BOOL didWriteSuccessfull = [data writeToFile:path atomically:YES];
    return didWriteSuccessfull;
}

+(NSArray*)readFromPlistFile:(NSString*)filename{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentsDirectory = [paths objectAtIndex:0];
    NSString * path = [documentsDirectory stringByAppendingPathComponent:filename];
    NSData * data = [NSData dataWithContentsOfFile:path];
    return  [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

@end //needs to be set for implementation
DAS
  • 1,941
  • 2
  • 27
  • 38
Saurabh Wadhwa
  • 1,173
  • 12
  • 16
  • I used this code on NSDictionary, and it worked great. Thanks! – Nathan Aug 10 '12 at 10:02
  • 2
    I have a custom class that has many different properties (`NSInteger`, `BOOL`, etc), stored in an `NSArray`. The `[NSArray writeToFile:atomically:]` didn't work, but converting the root object (the `NSArray`) to `NSData` with `NSKeyedArchiver` did work! – James Perih Oct 16 '14 at 18:56