如何更新 NSMutableDictionary。我的代码不起作用
我已经使用填充了一个数组。
arrSettings = [[NSMutableArray alloc] initWithContentsOfFile:[self settingsPath]];
该文件是一个 plist,其根为数组,然后是一个字典,其中三个键定义为数字。我也尝试过将键设置为字符串。
我使用在视图上显示 plist 文件中的值。
diaper = [[arrSettings objectAtIndex:0] objectForKey:@"Diaper Expenses"];
oil = [[arrSettings objectAtIndex:0] objectForKey:@"Oil Used"];
tree = [[arrSettings objectAtIndex:0] objectForKey:@"Wood Used"];
这段代码工作正常,字典中的值被分配给变量并显示它们。用户可以进行更改,然后按保存按钮。
我使用此代码提取数组的字典部分,以便我可以更新它。 对 editDictionary 的赋值有效。我已经仔细检查了键名(包括大小写),这是正确的。
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"];
在此示例中,我使用了 nsnumber。但我也尝试过将 xxxAmount 字段作为 SetValue 的一部分 而不是创建 NSNumber。这两种实现都不起作用。
发生了一些奇怪的事情。有时,前两个 setvalue 语句有效,但最后一个 setvalue 会失败并出现 EXC_BAD_ACCESS 故障。其他时候,第一个 setValue 会失败并出现相同的错误。我不知道为什么前两个有时会起作用。
我不知道下一步该做什么。我已经尝试了几种实现,但没有一个起作用。 另外,在调试器中如何显示 editDictionary 元素。我可以看到 editDictionary,但我不知道如何显示各个元素。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有 2 个 editDictionary 分配,第二个分配消除了第一个分配。也许您的意思是
editDictionary = [[arrSettings objectAtIndex:0] mutableCopy]
之类的东西?You have 2 assignments to editDictionary, and the second one wipes out the first. Maybe you meant something like
editDictionary = [[arrSettings objectAtIndex:0] mutableCopy]
?