gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:55:34 27 4
gpt4 key购买 nike

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

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

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

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

像这样:

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

谢谢

最佳答案

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

关于ios - 以编程方式将字典添加到 Plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26640424/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com