将数据附加到PLIST - iPhone [英] Appending data to PLIST - iPhone

查看:79
本文介绍了将数据附加到PLIST - iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要能够读入现有plist文件的内容,然后添加到数组/字典中,然后将这些新数据写入plist文件。狭窄很简单。我们想要多个根密钥作为数组,每个根密钥都有一些值。结构如下所示,

we need to be able to read in the contents of an existing plist file then add to the array / dictionary then write this new data to the plist file. The stricture is simple. We want multiple root keys as arrays and each with a few values. The structure looks like this,

124818240124810284.JPG          Array
                item 0          String     Some Attribute 1
                item 1          String     Some Attribute 2

到目前为止,我们可以创建上述内容而没有任何问题。但是,只要我们将另一个数组写入plist,文件就会被覆盖,所有当前内容都会丢失。我们需要做的是阅读上面的内容并添加它以便我们得到类似的东西,

So far we can create the above with no issue. But, as soon as we go to write another array to the plist the file is simply overwritten and all current contents are lost. What we need to do is read in the above and add to it so we get something like this,

124818240124810284.JPG          Array
                item 0          String     Some Attribute 1
                item 1          String     Some Attribute 2
354273577823597297.JPG          Array
                item 0          String     Some Attribute 1
                item 1          String     Some Attribute 2

依此类推。我们现在感到茫然,并且一直在努力奋斗这一天。请尽可能帮助你!在此先感谢!!

And so on. We're at a loss and have been struggling with this for a day now. Please help where you can! Thanks in advance!!

我们目前正在编写此文件如下

We are currently writing this file as follows

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Gallery.plist"];

// set the variables to the values in the text fields
NSMutableArray *myPhotos = [[NSMutableArray alloc] initWithCapacity:2];
[myPhotos addObject:@"NO"];
[myPhotos addObject:@"NO"];


// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray  arrayWithObjects: myPhotos, nil] forKeys:[NSArray arrayWithObjects: self.photoName, nil]];

NSString *error = nil;

// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

// check is plistData exists
if(plistData) {
    // write plistData to our Data.plist file
        [plistData writeToFile:plistPath atomically:YES];
}else{
        NSLog(@"Error in saveData: %@", error);
        [error release];
}

以下是我们最终的结果

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Gallery.plist"];

// Get current content.
NSDictionary *oldContent = [NSDictionary dictionaryWithContentsOfFile:plistPath];
// Make a mutable copy.
NSMutableDictionary *newContent = [[oldContent mutableCopy] autorelease];
// Add new stuff.

NSMutableArray *myPhotos = [[NSMutableArray alloc] initWithCapacity:2];
[myPhotos addObject:@"Attribute 1"];
[myPhotos addObject:@"Attribute 2"];

[newContent setObject:myPhotos forKey:self.filePath];

// Now, write the plist:
[newContent writeToFile:plistPath atomically:YES];


推荐答案

你这样做:

// Get current content.
NSDictionary *oldContent = [NSDictionary dictionaryWithContentsOfFile:plistPath];
// Make a mutable copy.
NSMutableDictionary *newContent = [[oldContent mutableCopy] autorelease];
// Add new stuff.
[newContent setObject:newObject forKey:newKey];
// Now, write the plist:
[newContent writeToFile:plistPath atomically:YES];

无需使用 NSPropertyListSerialization at都在这里

There is no need to use NSPropertyListSerialization at all here.

这篇关于将数据附加到PLIST - iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆