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

Posted

技术标签:

【中文标题】以编程方式将字典添加到 Plist【英文标题】:Adding dictionaries to Plist programmatically 【发布时间】:2014-10-29 20:49:00 【问题描述】:

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

根(字典) | StringOfViewID(字典) | 按钮标题(字典) | 细绳 字符串

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

目前我只能替换现有的。

类似这样的:

根(字典) | StringOfViewID(Dict) - ButtonTitle(Dict)(2)-String String | ButtonTitle(字典)(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];

谢谢

【问题讨论】:

我不太了解你的数据结构。我想你的字典太多了。看起来您可以继续将 buttonName 字典条目添加到此根字典中,并且不需要“StringOfViewID”字典。 我实际上需要所有字典。我正在获取视图的 ID(即标签),因此它是一个不同的字典,将在每个视图上保存许多按钮,并且每个按钮都需要有它的坐标和名称。至少这对我来说是有道理的,但如果你能提出一个更好的方法,请让我有点失落...... 您想在将 plist 文件读回内存后添加更多按钮,还是仅在第一次创建文件时添加? 不,我想在编写 plist 后添加更多按钮。不同的去每个视图。有些视图可能没有按钮,有些可能由用户添加。 【参考方案1】:

只需先将文件加载到 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 是当前感兴趣视图中的所有按钮都具有的键,而其他视图将具有其他键。

【讨论】:

我尝试复制它然后这样做:[viewID copy]; newViewID = [NSMutableDictionary mutableCopy];但我遇到了崩溃。 然后我尝试了这个: NSMutableDictionary *dictionaryWithExistingKey = [viewID objectForKey:@"StringOfViewID"]; NSMutableDictionary *dictionaryOldAndNewItems = [NSMutableDictionary dictionaryWithDictionary:dictionaryWithExistingKey]; [dictionaryOldAndNewItems addEntriesFromDictionary:buttonName]; [newViewID setObject:dictionaryOldAndNewItems forKey:@"StringOfViewID"]; [newViewID writeToFile:path atomically:YES];但什么也没发生 addEntriesFromDictionary 只要您使用相同的键,就会用新值替换旧值。您需要使用新键插入新的buttonName,或将其设为同一键下的按钮名称数组。如果我没有说清楚,我可以为您提供示例代码。 不胜感激......我试过这个:NSMutableDictionary *existingDictionary; [现有字典 addEntriesFromDictionary:viewID]; NSMutableDictionary *dictionaryOldAndNewItems = [NSMutableDictionary dictionaryWithDictionary:existingDictionary]; [dictionaryOldAndNewItems addEntriesFromDictionary:buttonName]; [newViewID setObject:dictionaryOldAndNewItems forKey:@"StringOfViewID"]; [newViewID writeToFile:path atomically:YES];没有成功 @GeorgeAsda 我已经为这两种方式添加了代码,请检查它们并告诉我是否有任何不清楚或无法解决问题。【参考方案2】:

我会创建一个类作为数据模型。这是一个简单的实现——创建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];

【讨论】:

以上是关于以编程方式将字典添加到 Plist的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式将字符串和键添加到 iPhone 中的字典?

以编程方式创建 plist

如何以编程方式编辑 plist?

无法以编程方式将字典数组中的行添加到 NSTable

如何以编程方式从 plist 添加和检索数据

是否可以在不替换的情况下以编程方式修改/更新 plist 文件?