如何更改 NSMutableDictionary int 值

发布于 2024-09-10 11:12:03 字数 842 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

客…行舟 2024-09-17 11:12:03

您需要将一个 NSNumber 对象放入字典中。
“selectedVisited”是什么数据类型?我想您在这一行不会收到错误:

 selectedVisited = visitedSegmentedControl.selectedSegmentIndex;

您选择的数据类型可能是 NSInteger。 (因为 selectedSegmentIndex 是一个 NSInteger)。

您需要将值放入字典中,如下所示:

NSNumber *newValue = [[NSNumber alloc] initWithInteger:selectedVisited];
[states setValue:newValue forKey:THERE_KEY];
[newValue release];

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:

 selectedVisited = visitedSegmentedControl.selectedSegmentIndex;

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:

NSNumber *newValue = [[NSNumber alloc] initWithInteger:selectedVisited];
[states setValue:newValue forKey:THERE_KEY];
[newValue release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文