- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个 plist,它被初始化为字典数组的数组。 plist 存储动态数据。因此,当应用程序终止时,可能需要添加新的字典元素。或者可能需要添加新的字典元素数组。稍后,字典元素可能会减少,从而不再需要以前的元素。虽然有很多关于向 plist 添加元素的讨论,但似乎没有一个能解决我的问题。以下是初始 plist 描述:
Key Type Value
Root Dictionary (2 items)
Hourly Data Array (1 item)
Item 0 Array (1 item) <--how do I add more of these
Item 0 Dictionary (3 items) <--how do I add more of these
Name String Joe
Rank String private
serialNo String 123456
NonHourly Data Array (1 item)
Item 0 Array (1 item)
Item 0 Dictionary (3 items)
Name String Jeff
Rank String private
serialNo String 654321
虽然我完全理解如何读取此 plist 文件,但我不明白如何将数组元素添加到每小时和非每小时数据数组,或者如何添加新的字典数组项,然后将它们写回 plist .
因此,以以下代码为起点,我如何完成此代码以添加上述数组元素和字典元素:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // Create a list of paths
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *configPlist = [documentsDirectory stringByAppendingPathComponent:@"config.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: configPlist];
...
[data writeToFile: configPlist atomically:YES];
最佳答案
好的,在尝试错误之后,我弄清楚了如何做到这一点。首先,让我为所存储的数据提供一个定义。该数据适用于所有具有小时工和非小时工身份的军人。 “每小时数据”和“非每小时数据”被视为 1 级数组。 1 级数组的元素称为 2 级数组。 2 级数组可以被认为是军人服役过的国家。2 级数组的元素是字典。每个词典都描述了一个军人。
现在对于答案,基本上我需要从内到外工作并创建一个包含要存储的数据的 2 级字典数组。然后将该 2 级数组添加到 1 级数组中。最后,级别 1 数组作为对象存储在 plist 的每小时数据或非每小时数据键中。解决办法如下:
// Create an array of arrays of content encoded as dictionary objects for hourly data
armyPerson *aPerson;
NSArray *level1Element;
NSMutableArray *level2Array;
NSMutableArray *level1Array = [[[NSMutableArray alloc] initWithObjects:nil] autorelease];
for (int i=0; i<[allHourlyPersons count]; i++) {
level1Element = [allHourlyPersons objectAtIndex:i]; // grab the next level1 array element.
// Each level1 array element will have zero or more level2Arrays. The info for each level2 dictionary needs to be collected into
// an level2Array element as individual dictionary objects
level2Array = [[[NSMutableArray alloc] initWithObjects:nil] autorelease]; // initialize the level2Array
for (int j=0; j<[level1Element count]; j++) {
aPerson = [level1Element objectAtIndex:j]; // grab the next armyPerson
// For each episode, create a dictionary object
NSDictionary *level2Element = [NSDictionary dictionaryWithObjectsAndKeys:aPerson.name, @"Name",
aPerson.rank, @"Rank",
aPerson.serialNo, @"serialNo", nil];
[level2Array addObject:level2Element]; // save the info for this episode
}
// Now that we have all the episodes for this level1Element collected into an array of dictionary objects,
// we need to add this array to the level1 array
[level1Array addObject:level2Array];
}
// Finally, we need to create the key-value pair for the hourly source array
[data setObject:level1Array forKey:@"Hourly Data"];
...
Do the same for the Non-Hourly Data prior to:
[data writeToFile: configPlist atomically:Yes];
关于objective-c - 对于包含字典数组的 plist,如何添加和写出新的字典元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6459995/
我在一个本土 C++ 框架内工作,其中一个类间接连接到 libjpeg-8c.so(作为 Ubuntu 16.04 突触包获得)。我在我的应用程序上运行 valgrind,它最终会写出图像数据,如下所
上下文 我正在运行一个 Tomcat 8.5 服务器,前端有一个 Nginx 反向代理来终止 SSL 连接,启用更多压缩。 在 Tomcat 服务器上,我有一个正在运行的 Web 应用程序,其中包含一
我正在尝试让 grunt-jsdoc-plugin 正常工作,但遇到了一个小问题。在我的控制台中,我不断收到: Running "jsdoc:dist" (jsdoc) task Warning: C
我继承了一个在数据库中存储 zip 文件的旧应用程序,需要检索该文件。在 Firefox 中运行良好,我可以打开 zip 并且其中的每个文件都很好。当我在 IE7 中运行它时,出现以下错误。 Inte
我想创建一个能够写出平面 html 文件的 cms,因此不需要数据库参与。 这个想法是CMS将允许编辑和更新文件(用php编写,如果需要的话可以使用mysql数据库),然后将这些更改保存/写出到htm
我有一个 javascript 函数,当通过 javascript 添加 td 元素时,它可以在 onclick 上正常运行。删除按钮工作正常。但是当我使用 php 创建元素并单击“删除”时,我得到:
我正在使用 node-png 库制作 png,然后将其保存在本地目录中,但是当我重新打开它时,它说它不存在。我想读入数据并将其发送出去,或者只是让响应发送一个 带图片的字段。这是我到目前为止所拥有的:
我需要一个类似于此处解释的函数... JS function for writing out a word, binary counter style ...但使用基数 7(或其他)生成(计数)从 A
我使用 matplotlib.pyplot 创建了一个简单的 hexbin 图。我没有更改任何默认设置。我的 x 轴信息范围从 2003 到 2009,而 y 值范围从 15 到 35。matplot
这是我的代码的重要部分: int realnum, positive = 0, total, poscount; for (poscount = 1; poscount > realnum;
我正在尝试在 Julia 中读取和写入一个简单的数据集。数据集是 mtcars ,取自 R,任意添加一列 bt带有随机 bool 值。文件/文件夹结构(如下)是使用 R arrow 写出的。包裹。 文
我正在尝试将数据写入包含日语字符的 Excel 文件。我正在使用 codec.open() 来获取数据,这似乎工作正常,但是当我尝试写入数据时遇到了这个错误: UnicodeEncodeError:
我是一名优秀的程序员,十分优秀!