escribir un diccionario para plist en iphone

I want to add a new dictionary to the existing plist file, how can I do that, I can read from the file, but with the same approach, writing doesn't work. My plist structure looks like this:

Root         Array
  |__item0   Dictionary
  |__item1   Dictionary
  |__item2   Dictionary

The code of writing to the file looks like this:

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

if (![fileManager fileExistsAtPath:plistPath]) {
    plistPath = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"];
}

NSMutableArray *dataRoot = [NSMutableArray arrayWithContentsOfFile:plistPath];

NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
[item setObject:@"value1" forKey:@"1"];
[item setObject:@"value2" forKey:@"2"];
[item setObject:@"value3" forKey:@"3"];
[dataRoot addObject:item];
[dataRoot writeToFile:plistPath atomically:YES];
[item release];

Can anyone help me with this, thanks!

I have updated my code, but it still doesn't work...And the problem is list.plist file doesn't get copied to ~/Documents directory.

preguntado el 28 de septiembre de 11 a las 05:09

4 Respuestas

I think code is not wrong.

Have you check plistPath, dataRoot, and item?

They have correct value?

Respondido el 28 de Septiembre de 11 a las 09:09

I guess this is the easiest structure I can think of, there is one thing I dont understand, if I have multiple levels between root and dictionaries, how does my program know where to write a new dictionary? - Michael

@Michael If you read file as nsmutablearray. multi levels plist is not cosiderable thing. program add object to nsmutablearray. - ChangUZ

@Michael and How to get plistPath is not good method. Use NSSearchPathForDirectoriesInDomains function. - ChangUZ

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"list.plist"];


if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {


bingoArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];

}

[bingoArray writeToFile:filePath atomically:YES];

Respondido el 28 de Septiembre de 11 a las 09:09

You are trying to write the main bundle which is no-no on iOS. Please read the Algunos directorios de aplicaciones importantes.

But your coding boils down to the following:

  1. Check if plist is not in Application_Home/Documents/
  2. Copied original plist from main bundle is the answer to step 1 is false.
  3. Open the plist in the Documentos directorio
  4. Update NSMutableArray
  5. Write to file the plist

Respondido el 28 de Septiembre de 11 a las 10:09

please refer to my updates, I still cannot get it work, thanks ~ - Michael

The code in your question is still point the file in the main bundle. You didn't do step 2 in my answer which is to copy the plist from the main bundle to the documents folder. - Rana negra

Yes, this is where I dont understand, I thought if loop does the copy, can you show me some code of how to copy from bundle to local and backwards? - Michael

There is plenty of code sample out there on this matter. For example: iPhone (iOS): error al copiar archivos del paquete principal a la carpeta de documentos - Rana negra

You can only save primitive types by default.

try out this link.

http://deadpanic.com/howtosave

He explains how to save any object to disk by using NSCoding protocol.

If you are working only with primitives then I apologize for wasting your time. But this is the path I took to write my custom stuff to disk. And I thought it was just an array issue.

Edit:

I just noticed... It looks like you are trying to write to an array in the Main Bundle. These files are read only.

You need to save a copy of the file in the Documents directory.

try this out to find the documents directory

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// Change your plistPath to be this
plistPath = [documentsDirectory stringByAppendingPathComponent:@"list.plist"];
[dataRoot writeToFile:plistPath atomically:YES];

put your plist in this folder and you can write to it.

Respondido el 28 de Septiembre de 11 a las 22:09

Sorry, I dont quite understand these three lines of code, what's "ctory"? Is this a directory that contains the plist? Where do I set this directory? - Michael

I corrected the spelling error. Volure meant to type NSDocumentDirectory. - Rana negra

Now I just added these three lines before the code above, but I dont know how to do the rest, can you show me some example? - Michael

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.