【问题标题】:How to change a NSMutableDictionary int value如何更改 NSMutableDictionary int 值
【发布时间】:2010-07-17 17:24:48
【问题描述】:

我有一个 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 目录并保存。这里有什么问题?

【问题讨论】:

    标签: iphone save plist nsmutabledictionary documents


    【解决方案1】:

    您需要将 NSNumber 对象放入 Dictionary 中。 “selectedVisited”是什么数据类型?,我猜你在这一行没有收到错误:

     selectedVisited = visitedSegmentedControl.selectedSegmentIndex;
    

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      相关资源
      最近更新 更多