xonegirlz
xonegirlz

Reputation: 8967

issue in saving NSdictionary to disk using NSFileManager

I am constantly getting the following error:

saving dictionary error Property list invalid for format (property lists cannot contain objects of type 'CFNull')

when I try to save a file using the code as follows:

- (BOOL)saveDictionary:(NSDictionary *)dict toFile:(NSString *)file {
    BOOL success = NO;
    if (file == nil || dict == nil) return NO;

    NSString *errorStr = nil;
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:dict
                                                                   format:NSPropertyListBinaryFormat_v1_0
                                                         errorDescription:&errorStr];
    if (errorStr) {
        // Darn.
        NSLog(@"saving dictionary error %@", errorStr);
        [errorStr release];
    } else {
        //    [plistData writeToFile:file atomically:YES];
        // (ankit): Changing this to NO for speed.
        success = [plistData writeToFile:file atomically:NO];
    }

    return success;
}

any idea why?

Upvotes: 0

Views: 2062

Answers (1)

TheAmateurProgrammer
TheAmateurProgrammer

Reputation: 9392

That method is perfectly fine, it's the content inside your NSDictionary that's causing that error. Since you're saving it as a plist file, everything inside the dictionary should only contain NSString's, NSDate's, NSArray's, NSDictionary's, NSNumber's and NSData's. Anything else other than these will result in the error similar to the one you have above. So go over what you have inside your dictionary, and if any of your object's class are not part of the above list, put the objects into NSData first before you put it in into the dictionary.

Upvotes: 3

Related Questions