开发者

How to update NSMutableDictionary. My code doesn't work

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 numberW开发者_高级运维ithFloat: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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜