如何更改 NSMutableDictionary int 值
我有一个 StateArray.plist 设置了 50 个字典条目,每个字典条目包含一个带有状态名称和数字 0 的字符串。我想要做的是通过分段控件将 0 更改为 1 并保存更新后的值plist。我的代码如下,不会保存更改后的号码。
-(void) viewWillAppear: (BOOL) animated
{
[super viewWillAppear:animated];
nameOfStateTextField.text = [states objectForKey:NAME_KEY];
}
//segmented control
-(IBAction)visitedChanged
{
selectedVisited = visitedSegmentedControl.selectedSegmentIndex;
}
-(IBAction)saveButton
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"StateArray.plist"];
[states setValue:selectedVisited forKey:THERE_KEY];
[states writeToFile:path atomically:YES];
}
更改的分段控制值最终应作为新的 THERE_KEY 并写入 Documents 目录并保存。这里可能出了什么问题?
I have a StateArray.plist set up with 50 dictionary entries each containing a string with the name of the state and a number 0. What I want to be able to do is change the 0 to a 1 through a segmented control and save the updated plist. The code I have is below and won't save the changed number.
-(void) viewWillAppear: (BOOL) animated
{
[super viewWillAppear:animated];
nameOfStateTextField.text = [states objectForKey:NAME_KEY];
}
//segmented control
-(IBAction)visitedChanged
{
selectedVisited = visitedSegmentedControl.selectedSegmentIndex;
}
-(IBAction)saveButton
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"StateArray.plist"];
[states setValue:selectedVisited forKey:THERE_KEY];
[states writeToFile:path atomically:YES];
}
The changed segmented control value should end up as the new THERE_KEY and be written to the Documents directory and saved. What could be wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将一个 NSNumber 对象放入字典中。
“selectedVisited”是什么数据类型?我想您在这一行不会收到错误:
您选择的数据类型可能是 NSInteger。 (因为 selectedSegmentIndex 是一个 NSInteger)。
您需要将值放入字典中,如下所示:
You need to put a NSNumber Object into the Dictionary.
What datatype is "selectedVisited"?, i guess you don't get an error at this line:
the datatype you choose is probably a NSInteger. (Since selectedSegmentIndex is a NSInteger).
You need to put the value into the dictionary something like this: