views:

54

answers:

1

I've populated an array using.

arrSettings = [[NSMutableArray alloc] initWithContentsOfFile:[self settingsPath]];

The file is a plist with the root as an array and then a dictionary with three three keys defined as number. I've also tried setting the keys to string.

I display the values in the plist file on a view using.

diaper = [[arrSettings objectAtIndex:0] objectForKey:@"Diaper Expenses"];
oil = [[arrSettings objectAtIndex:0] objectForKey:@"Oil Used"];
tree = [[arrSettings objectAtIndex:0] objectForKey:@"Wood Used"]; 

This code works fine, the values in the dictionary are assigned to the variables and they are displayed. The user can make changes and then press a save button.

I use this code to extract the dictionary part of the array so I can update it. The assignment to editDictionary works. I've double checked the key names including case and that is correct.

editDictionary = [[NSMutableDictionary alloc] init];
editDictionary = [arrSettings objectAtIndex:0];
NSNumber *myNumber = [NSNumber numberWithFloat:diaperAmount];
[editDictionary setValue:myNumber forKey:@"Diaper Expenses"];
myNumber = [NSNumber numberWithFloat:oilAmount];    
[editDictionary setValue:myNumber forKey:@"Oil Used"];
myNumber = [NSNumber numberWithFloat:treeAmount];
[editDictionary setValue:myNumber forKey:@"Wood Used"];

In this example I've used a nsnumber. But I've also tried the xxxAmount field as part of SetValue instead of creating a NSNumber. Neither implementation works.

Several strange things happen. Sometimes the first two setvalue statements work, but the last setvalue fails with a EXC_BAD_ACCESS failure. Other times the first setValue fails with the same error. I have no idea why the first two sometimes work.

I'm at a loss of what to do next. I've tried several implentations and none of them work. Also, in the debugger how can I display the editDictionary elements. I can see editDictionary, but I don't know how to display the individual elements.

+2  A: 

You have 2 assignments to editDictionary, and the second one wipes out the first. Maybe you meant something like editDictionary = [[arrSettings objectAtIndex:0] mutableCopy]?

JWWalker
I don't understand how I have two assignments. There is an alloc and then an assignment. Are you saying the alloc is an assignment in this case. Doesn't an object like a dictionary require an alloc before an assignment can be made to it?This could have been combined on one line but I usually use two. [[NSMutableDictionary alloc] initwithArray [arrSettings objectAtIndex:0]];
You have 2 lines beginning 'editDictionary ='. Those are 2 assignments. I guess you meant to set the contents of your dictionary in the second one, but that's not what you did, you just switched to a different dictionary.
JWWalker