IPhone: How to write plist array of dictionary object


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

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:


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!

Best Solution

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"];