0

I'm banging my head on a problem for hours, so be good..

I have created a custom plist for various upgrades of the game, this is structure of this 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)

each value with the key "packX" is put on the respective buttons,

if a button is clicked, I want to change and save the value "purchasedX" of their respective "packX" associated with the button touched.

EXAMPLE:

Touch the button associated with "pack3", at this point "purchased3" changes to another value.

MY CODE: CHANGED AFTER ANSWER OF @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

how can I do this?

if you need any other info just ask

thanks everybody

Ilario
  • 5,979
  • 2
  • 32
  • 46
  • Have you looked at this answer? http://stackoverflow.com/questions/12274530/updating-and-saving-data-in-plist. It is about how to correctly get the plist data and put it in a dictionary like you have. Then save the dictionary to a plist-file like you want. The only thing it lacks is the actual updating of an object in the dictionary but i'm sure you can manage that? – Totumus Maximus Apr 22 '14 at 14:37
  • @TotumusMaximus thanks for link, but now my problem is how to replace that particular object based on button touched.. :-/ – Ilario Apr 22 '14 at 14:41
  • Well each button should have either a different target function OR each button can be given a tag with a unique (!) id. So that you can figure out which button is which pack. Then you can just iterate of the pack-array within that Root-dictionary and overwrite the value at a specific key. That should do the trick, right? – Totumus Maximus Apr 22 '14 at 14:44
  • @TotumusMaximus i've edited again.. ;) – Ilario Apr 22 '14 at 14:57
  • Each item in ArrayPack will be a dictionary with 3 key/values. The keys are "pack1", "pack1bonus", "purchased1" (as example). You want to update one (or all) of these key/values. If this is correct and you can access the dictionary everywhere and you can loop over the arrayPacks until you find the one that matches the tag. Then the only thing you need to do is replace the key/value pair with the new value. [dictionary setObject:@"value" forKey:@"key"]; And since you work with the real dictionary. You can just call this function and still write the dictionary to file. – Totumus Maximus Apr 22 '14 at 15:08
  • updating key/value pairs in dictionaries, ref: http://stackoverflow.com/questions/5736605/replacing-key-value-in-nsdictionary – Totumus Maximus Apr 22 '14 at 15:08

1 Answers1

2

You have several things to deal with.

I gather that when the user pushes button X, you want to change a value in your "pack comfort "array, at index X?

You need to deal with the fact that plists are read in from disk with all the objects inside immutable. You will need to convert your plist structure to mutable.

You need logic to respond to a button press and update the correct object in your array.

The simple way to deal with this is to put tags on all your buttons. I would suggest using tags 100, 101, 102, etc. Then subtract 100 from the tag and use it as an array index.

Here is some rough code to convert your whole plist to 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;
}

Note that the code above is for illustration purposes. It has never been compiled, much less tested. It probably contains syntax errors, and may contain bugs. Getting it running will be up to you.

Once you have a bunch of buttons with tags, you'll need to write an action method that figure out which array element to change, and then updates the data in that element.

The button IBAction code might look like this:

- (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;
}
Ilario
  • 5,979
  • 2
  • 32
  • 46
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • doing so also changes the plistDict, but I have not noticed immediately, thank you very much! – Ilario Apr 22 '14 at 15:57