4

I looked around other similar questions about this but nothing really worked.

I managed to write only one pair (object / key) of the dictionary (e.g.: setObject:itemProperties[0] forKey[0]) on my Plist. But I would like all the objets and keys to be added. I didn't managed so far (return the error). Any help?

 // Path to Documents Folder

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"items.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path])
{
    path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"items.plist"] ];
}

NSMutableDictionary *items;

if ([fileManager fileExistsAtPath: path])
{
    items = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
}
else
{
    // If the file doesn’t exist, create an empty dictionary
    items = [[NSMutableDictionary alloc] initWithCapacity:5];
}

// array of the item properties
NSArray *itemProperties = @[myItem.itemTitle, myItem.itemImage, myItem.itemPositionX, myItem.itemPositionY, myItem.itemHeight, myItem.itemWidth];

// Set the key values for each field    
NSArray *keys = @[@"Title", @"Image", @"PositionX", @"PositionY", @"Height", @"Width"];


[items setObject:itemProperties forKey:keys];

//Save dictionnary to Plist
[items writeToFile: path atomically:YES];

if (![items writeToFile:path atomically:YES]) {
    NSLog(@"Error with creating Plist");
}
Xavier
  • 270
  • 1
  • 7
  • 17

2 Answers2

18

You can not control the content you are going to write sometimes. For example, you can't avoid a null value when you are going to write a JSON object that is gotten from a server.

NSData is compatible with these "invalid" values, so converting NSArray or NSDictionary to NSData is an ideal way in these cases.

write:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:jsonObject];
[data writeToFile:path atomically:YES];

read:

NSData *data = [NSData dataWithContentsOfFile:path];
NSDictionary *jsonObject = [NSKeyedUnarchiver unarchiveObjectWithData:data];
Brian
  • 30,156
  • 15
  • 86
  • 87
  • 2
    Instead of copy/pasting your answer from [Why NSMutableDictionary don't want write to file?](http://stackoverflow.com/questions/8713846/why-nsmutabledictionary-dont-want-write-to-file/20659846#20659846), you should flag this question as a duplicate of the one you originally answered (if it really is one). If it is not a duplicate, than your answer is invalid. – newfurniturey Dec 18 '13 at 14:07
  • Sorry I didn't know I can flag a question as a duplicate. I'll do it in the future. – Brian Dec 19 '13 at 03:37
  • Help me a lot, this works well! – Gank Nov 26 '14 at 01:33
  • that was our problem, that was the solution ! – Enrico Cupellini Sep 03 '15 at 09:58
8

Have you properly use valid keys & values ?

This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

Reference : writeToFile:atomically:

Coolant
  • 448
  • 6
  • 13