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