以编程方式将词典添加到Plist

时间:2014-10-29 20:49:00

标签: ios nsdictionary plist

我正在尝试以下列格式以编程方式将字典添加到plist中:

根(字典) | StringOfViewID(字典) | 按钮名称(字典) | 串 串

我可以成功地做到这一点,但我想在同一ViewID(Dict)下继续向ViewID(Dict)添加更多ButtonTitle(Dict)。

到目前为止,我只能替换现有的。

这样的事情:

根(字典) | StringOfViewID(Dict) - ButtonTitle(Dict)(2)-String String | 按钮名称(字典)(1) | 串 串

以下是我使用的代码:

//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];

由于

2 个答案:

答案 0 :(得分:2)

只需将文件首先加载到NSMutableDictionary,然后进行所需的更改,然后使用您已使用的相同代码将其写回文件。

编辑:关于用于编辑列表的结构,您可以使用按钮词典数组

   NSMutableDictionary* oldDictionary=[NSMutableDictionary dictionaryWithContentsOfFile:path];

   // make the new button dictionary
   NSNumber *xNumber = [[NSNumber alloc] initWithDouble:locationX];
   NSNumber *yNumber = [[NSNumber alloc] initWithDouble:locationY];

   NSDictionary*buttonDictionary=@{@"locationX": xNumber,
                                   @"locationY":yNumber,
                                   @"myButtonTitle":myButtonTitle};
   NSMutableArray *buttonsArray = [oldDictionary objectForKey:@"StringOfViewID"];
   //append it to the array
   [buttonsArray addObject:buttonDictionary];
   //replace the old array with the new one
   [oldDictionary setObject:buttonsArray forKey:@"StringOfViewID"];
   //write it back to the file
   [oldDictionary writeToFile:path atomically:YES];

或按钮词典词典

    NSMutableDictionary* oldDictionary=[NSMutableDictionary dictionaryWithContentsOfFile:path];

    // make the new button dictionary
    NSNumber *xNumber = [[NSNumber alloc] initWithDouble:locationX];
    NSNumber *yNumber = [[NSNumber alloc] initWithDouble:locationY];

    NSDictionary*buttonLocationDictionary=@{@"locationX": xNumber, @"locationY":yNumber};
    NSMutableDictionary *buttonsDictionary = [oldDictionary objectForKey:@"StringOfViewID"];
    //add it to the dictionary
    [buttonsDictionary  setObject:buttonLocationDictionary forKey:myButtonTitle];//be sure that this is a new button title,or else it will replace the old value with this title.

    //replace the old dictionary with the new one
    [oldDictionary setObject:buttonsDictionary forKey:@"StringOfViewID"];
    //write it back to the file
    [oldDictionary writeToFile:path atomically:YES];

我试图不改变您正在使用的结构或键,我认为StringOfViewID是当前关注视图中所有按钮都具有的键,而其他视图将具有其他键。

答案 1 :(得分:1)

我会创建一个类作为数据模型。这是一个简单的实现 - 创建Button对象可能更简洁,而不是传递多个参数并检索字典

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

您可以按如下方式使用此类 -

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