Search code examples
iosnsdictionaryplist

Adding dictionaries to Plist programmatically


I am trying to add a dictionary to a plist programmatically in the following format:

Root(Dict) | StringOfViewID(Dict) | ButtonTitle(Dict) | String String

I can successfully do this but I want to keep adding to the ViewID(Dict) more ButtonTitle(Dict) under the same ViewID(Dict).

So far I can only replace the existing.

Something like this:

Root(Dict) | StringOfViewID(Dict) - ButtonTitle(Dict)(2)-String String | ButtonTitle(Dict)(1) | String String

Here is the code I'm using:

//Initialize and load the plist file here: 
[...]
            NSMutableDictionary *data;
            NSMutableDictionary *viewID;
            NSMutableDictionary *buttonName;
            NSArray *keys;
            NSArray *locations;


        // Insert the data into the plist

        NSNumber *xNumber = [[NSNumber alloc] initWithDouble:locationX];
        NSNumber *yNumber = [[NSNumber alloc] initWithDouble:locationY];

        keys = [NSArray arrayWithObjects:@"locationX", @"locationY", nil];
        locations = [NSArray arrayWithObjects:xNumber, yNumber, nil];
        data = [NSMutableDictionary dictionaryWithObjects:locations forKeys:keys];
        buttonName = [NSMutableDictionary dictionaryWithObject:data forKey:myButtonTitle];
        viewID = [NSMutableDictionary dictionaryWithObject:buttonName forKey:@"StringOfViewID"];

        [viewID writeToFile:path atomically:YES];

Thanks


Solution

  • I would create a class to serve as a data model. This is a simple implementation - it would probably be cleaner to create a Button object rather than pass multiple parameters and retrieve a dictionary

    ViewButtons.h

    @interface ViewButtons : NSObject
    
    +(ViewButtons *) viewButtonsWithContentsOfFile:(NSString *)file;
    
    -(void) addButton:(NSString *)buttonName withX:(double) x andY:(double) y toView:(NSString *)viewName;
    -(NSArray *)viewNames;
    -(NSArray *)buttonNamesForView:(NSString *)viewName;
    -(NSDictionary *)buttonWithName:(NSString *)name inView:(NSString *)viewName;
    -(void)writeToFile:(NSString *)file;
    
    @end
    

    ViewButtons.m

    #import "ViewButtons.h"
    
    @interface ViewButtons ()
    
    @property (nonatomic,strong) NSMutableDictionary *viewButtons;
    
    @end
    
    @implementation ViewButtons
    
    -(id) init {
        if (self=[super init]) {
            self.viewButtons=[NSMutableDictionary new];
        }
        return self;
    }
    
    +(ViewButtons *) viewButtonsWithContentsOfFile:(NSString *)file {
        ViewButtons *newViewButtons=[ViewButtons alloc];
        newViewButtons.viewButtons=[NSMutableDictionary dictionaryWithContentsOfFile:file];
        return newViewButtons;
    }
    
    -(void) addButton:(NSString *)buttonName withX:(double) x andY:(double) y toView:(NSString *)viewName {
        NSMutableDictionary *viewDict=self.viewButtons[viewName];
        if (viewDict == nil) {
            viewDict=[NSMutableDictionary new];
            self.viewButtons[viewName]=viewDict;
        } else if (![viewDict isKindOfClass:[NSMutableDictionary class]]) {
            viewDict=[viewDict mutableCopy];
            self.viewButtons[viewName]=viewDict;
        }
        NSNumber *xNumber = [NSNumber numberWithDouble:x];
        NSNumber *yNumber = [NSNumber numberWithDouble:y];
        NSDictionary *buttonDict=@{@"locationX":xNumber,@"locationY":yNumber};
        viewDict[buttonName]=buttonDict;
    }
    
    
    -(NSArray *)viewNames {
        return self.viewButtons.allKeys;
    }
    
    -(NSArray *)buttonNamesForView:(NSString *)viewName {
        return [self.viewButtons[viewName] allKeys];
    }
    -(NSDictionary *)buttonWithName:(NSString *)name inView:(NSString *)viewName {
        return self.viewButtons[viewName][name];
    }
    
    -(void)writeToFile:(NSString *)file {
        [self.viewButtons writeToFile:file atomically:YES];
    }
    
    @end
    

    You can use this class as follows -

    ViewButtons *viewButtons=[ViewButtons viewButtonsWithContentsOfFile:buttonFile];
    if (viewButtons == nil) {
        viewButtons=[ViewButtons new];
    }
    
    [viewButtons addButton:@"MyButton1" withX:0 andY:0 toView:@"MyView"];
    [viewButtons addButton:@"MyButton2" withX:1 andY:1 toView:@"MyView"];
    [viewButtons addButton:@"MyButton3" withX:0 andY:0 toView:@"MySecondView"];
    [viewButtons addButton:@"MyButton4" withX:0 andY:1 toView:@"MyThirdView"];
    [viewButtons writeToFile:buttonFile];