更新自定义嵌套plist中的值

时间:2014-04-22 14:22:49

标签: ios objective-c plist

我在问题上敲了好几个小时,所以要好......

我为游戏的各种升级创建了一个自定义plist,这是plist的结构:

 Root  (Dictionary)

  Pack Comfort  (Array)

   Item 0  (Dictionary)
    pack1         (String)
    pack1bonus    (String)
    purchased1    (Number)

   Item 1  (Dictionary)
    pack2         (String)
    pack2bonus    (String)
    purchased2    (Number)

   Item 2  (Dictionary)
    pack3         (String)
    pack3bonus    (String)
    purchased3    (Number)

每个值都带有键" packX"被放在相应的按钮上,

如果单击一个按钮,我想更改并保存值" purchaseX"他们各自的" packX"与触摸的按钮相关联。

示例

触摸与" pack3"相关联的按钮,此时"已购买3"改变另一个值。

我的代码: 在收到消息后 @DuncanC

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"comfort" ofType:@"plist"];
NSMutableDictionary *obj = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

//nsmutabledictionary declared before
plistDict = [[NSDictionary dictionaryWithContentsOfFile:filePath] mutableCopy];
NSMutableArray *anArray = [plistDict[@"Pack comfort"] mutableCopy];
plistDict[@"Pack comfort"] = anArray;

for (int index = 0; index < anArray.count; index++) {

    NSMutableDictionary *aDict = [anArray[index] mutableCopy];
    anArray[index] = aDict;
}

//NSArray declared before
arrayPack = obj[@"Pack Comfort"];

NSString *stringPack1 = [[arrayPack valueForKey:@"pack1"] objectAtIndex:0];
//this is value for button1, and until everything is right

UIButton *button1 = ...
button1.tag = 0;

...

-(void)buttonTarget:(id)sender {

 UIButton *button = sender;

 NSString *stringPurchased = [NSString stringWithFormat:@"purchased%i",button.tag+1];
 NSMutableArray *array = plistDict[@"Pack comfort"];
 NSMutableDictionary *aDict = array[button.tag];

 int newValue = 1;
 NSNumber *newNumber = @(newValue);

 aDict[stringPurchased] = newNumber;

 //here the new dictionary "plistDict" has changed, and save it in the app

我该怎么做?

如果您需要任何其他信息,请询问

谢谢大家

1 个答案:

答案 0 :(得分:2)

你有几件事要处理。

我认为,当用户按下按钮X时,您想要在索引X处更改“pack comfort”数组中的值吗?

你需要处理这样一个事实:plists是从磁盘读入的,其中所有对象都是不可变的。您需要将plist结构转换为可变结构。

您需要逻辑来响应按下按钮并更新阵列中的正确对象。

处理此问题的简单方法是在所有按钮上添加标签。我建议使用标签100,101,102等。然后从标签中减去100并将其用作数组索引。

以下是将整个plist转换为mutable的粗略代码:

NSMutableDictionary *plistDict;

//load the outer dictionary and make it mutable.
plistDict = [[NSDictionary dictionaryWithContentsOfFile: path] mutableCopy];

//Convert the Pack Comfort array to mutable and save it back to the outer dictionary
NSMutableArray *anArray = [plistDict[@"Pack Comfort"] mutableCopy];
plistDict[@"Pack Comfort"] = anArray;

for (int index = 0; index < anArray.count; index++)
{
  NSMutableDictionary *aDict = [anArray[index] mutableCopy];
  anArray[index] = aDict;
}

请注意,上面的代码仅用于说明目的。它从未编译过,也没有经过测试。它可能包含语法错误,可能包含错误。让它运行将取决于你。

一旦你有一堆带标签的按钮,你需要编写一个动作方法,找出要更改的数组元素,然后更新该元素中的数据。

按钮IBAction代码可能如下所示:

- (IBAction) handleButton: (UIButton *) sender;
{
  int index = sender.tag - 100;  //Convert the  button tag to a zero-based array index.

  //Load the array from the outer dictionary
  NSMutableArray *array = plistDict[@"Pack Comfort"];

  //Get the dictionary for this index
  NSMutableDictionary aDict = array[index];

  //Create a key for the item "purchasedx" in your inner dictionary
  NSString key = [NSString stringWithFormat: @"purchased%d", index];
  int newValue = x; //Whatever new value you want based on the button press.

  //Create an NSNumber containing the new value to be save in the dictionary
  NSNumber *newNumber = @(newValue)

  //replace the entry at key "purchasedx"
  aDict[key] = newNumber;
}