我们需要能够读取现有plist文件的内容,然后添加到数组/字典中,然后将这些新数据写入plist文件。狭窄很简单。我们需要多个根键作为数组,每个键都有一些值。结构看起来像这样,将数据附加到PLIST - iPhone
124818240124810284.JPG Array
item 0 String Some Attribute 1
item 1 String Some Attribute 2
到目前为止,我们可以创建上述没有问题。但是,一旦我们写入另一个数组到plist,文件就会被覆盖,所有当前内容都将丢失。我们需要做的是在上面阅读,并添加到它,所以我们得到这样的事情,
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
等等。我们目前处于亏损状态,并且一直在为此奋斗一天。请尽可能帮助!提前致谢!!
目前,我们正在写这个文件如下
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];
因此,实质上你有麻烦添加另一个条目到数组?伪代码:'array = [self readPlist]; array = [array arrayByAppendingObject:newEntry]; [self writePlist:array];'那是你想要做的吗? – DarkDust 2012-01-16 18:01:40
本质上是的,但我们在创建可变数组时遇到了麻烦,增加了数据然后写出来。我想发布我们目前如何编写文件,如果这将有助于 – mejim707 2012-01-16 18:08:51
是的,请编辑您的问题,并发布导致麻烦的代码。 – DarkDust 2012-01-16 18:16:47