Objective c 对于包含字典数组的plist,如何添加和写出新的字典元素
我创建了一个plist,它被初始化为字典数组的数组。plist存储动态数据。因此,当应用程序终止时,可能需要添加新的字典元素。或者可能需要添加字典元素的新数组。以后,可能会有更少的字典元素,因此不再需要以前的元素。虽然有很多关于向plist添加元素的讨论,但似乎没有一个能够解决我的问题。以下是plist的初始描述:Objective c 对于包含字典数组的plist,如何添加和写出新的字典元素,objective-c,ios,plist,Objective C,Ios,Plist,我创建了一个plist,它被初始化为字典数组的数组。plist存储动态数据。因此,当应用程序终止时,可能需要添加新的字典元素。或者可能需要添加字典元素的新数组。以后,可能会有更少的字典元素,因此不再需要以前的元素。虽然有很多关于向plist添加元素的讨论,但似乎没有一个能够解决我的问题。以下是plist的初始描述: Key Type Value Root Dictionary (2 items) Hourl
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
好的,在尝试了一个错误之后,我想出了如何做到这一点。首先,让我为存储的数据提供一个定义。该数据适用于具有每小时和非每小时状态的所有陆军人员。“每小时数据”和“非每小时数据”被视为1级阵列。级别1数组的元素称为级别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];
//为每小时数据创建一个编码为字典对象的内容数组
armyPerson*aPerson;
NSArray*1级元素;
NSMUTABLEARRY*level2Array;
NSMutableArray*level1Array=[[NSMutableArray alloc]initWithObjects:nil]autorelease];
对于(int i=0;i
// 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];