从现有的plist创建一个新的plist?

时间:2013-07-18 07:29:08

标签: ios objective-c search plist

我在下面的第一张图片中有一个plist,这就是我的主要帖子,并且字段“bid”是唯一的。我的出价值(例如出价= 90)然后我需要搜索我的主要(第一张图片)plist 的所有条目中的值,并创建一个新的plist&检索相应的值(出价,说明,类别,评级)属于那个plist。我该怎么做?

修改

如果我有多个值,比如bid = 88而另一个出价= 90,那么我必须从主plist创建一个新的plist(如第一张图片所示)然后结果应该是喜欢图片3

First image-main plist

Final out put iam expecting

image3-new result

2 个答案:

答案 0 :(得分:1)

NSString *path = [[NSBundle mainBundle] pathForResource:@"mainPlistFile" ofType:@"plist"];
//path has path for main plist.

NSDictionary *dictionary=[NSDictionary dictionaryWithContentOfFile: path];
NSArray *listbook = [dictionary objectForKey:@"listbook"];

for (NSDictionary *item in listbook) {
if ([[item objectForKey:@"bid"] isEqualToString yourNSStringWithBidToSearch]) {
//now item is the desired dictionary, you can get values by using objectForKey
//or write it to a file like below.
[item writeToFile:pathToSaveNewPlist automatically:NO];
break;
     }
}

请记住,您必须先创建NSString pathToSaveNewPlist和yourNSStringWithBidToSearch,我会留给您。



所以你编辑了这个问题: 让我们假设您有一个NSArray的bidToSearch,其中包含要搜索的出价值的NSStrings:

NSMutableArray *targetListbook = [[NSMutableArrray alloc] init]];
NSDictionary *dictionary=[NSDictionary dictionaryWithContentOfFile:[[NSBundle mainBundle] pathForResource:@"mainPlistFile" ofType:@"plist"]];
for (NSDictionary *item in [dictionary objectForKey:@"listbook"])
    if ([bidsToSearch containsObject:[item objectForKey:@"bid"]])
        [targetListbook addObject:item];
[@{listbook:targetListbook} writeToFile:pathToSaveNewPlist automatically:NO];

答案 1 :(得分:0)

您可以使用NSPropertyListSerialization类来序列化和反序列化属性列表。

编辑:下面是一些如何做的伪代码。

NSData* data = [NSData dataWithContentsOfFile:plistFile]; // read file
id plist = [NSPropertyListSerialization propertyListWithData:data ...]; // deserialize
// do your work
data = [NSPropertyListSerialization dataWithPropertyList:plist2 ...]; // serialize
[data writeToFile:plistFile2 ...]; // write to file