views:

788

answers:

2

Hello, I'm a young Italian developer for the iPhone. I have a plist file (named "Frase") with this structure:

Root Array
 - Item 0         Dictionary
      Frase        String
      Preferito    Bool
 - Item 1         Dictionary
      Frase        String
      Preferito    Bool
-  Item 2         Dictionary
      Frase        String
      Preferito    Bool
 - Item 3         Dictionary
      Frase        String
      Preferito    Bool
exc.

An array that contains many elements dictionary, all the same, consisting of "Frase" (string) and "Preferito" (BOOL).

The variable "indirizzo", increase or decrease the click of the button Next or Back. The application interface:

http://img691.imageshack.us/img691/357/schermata20100418a20331.png

When I click on AddPreferito button, the item "Preferito" must be YES. Subsequently, the array must be updated with the new dictionary.The code:

(void)addpreferito:(id)sender {

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Frase" ofType:@"plist"];
MSMutableArray *frase = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
NSMutableDictionary *dictionary = [frase objectAtIndex:indirizzo];
[dictionary setValue: YES forKey:@"Preferito"];
[frase replaceObjectAtIndex:indirizzo withObject:dictionary];
[frase writeToFile:plistPath atomically:YES];

}

Why not work? Thanks Thanks Thanks!

A: 

You are loading your initial plist from inside your application bundle. Unfortunately, you cannot write to this directory. When you need to do is check if your plist already exists in the user documents directory. If it does, load it from there. If not, load your template (original) from inside the application bundle. When you write the file back, you must always write it to the user documents directory.

For more information on managing your application's files, please see the Files and Data Management section of the iPhone Application Programming Guide.

Jason Coco
A: 

What Jason said is correct, but it looks like there's another, more serious problem in your code: it appears that you're passing a primitive value (the defined constant YES) as the first argument to -setValue:forKey:, which expects an argument of type id (in other words, an object, not a primitive).

Instead, you can use an instance of NSNumber to wrap the boolean value, and then put it in the array, like so:

[dictionary setValue:[NSNumber numberWithBool:YES] forKey:@"Preferito"];
jlehr