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
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;
}