我们需要能够读入现有plist文件的内容,然后添加到数组/字典中,然后将此新数据写入plist文件。狭窄很简单。我们想要多个根密钥作为数组,每个根密钥都有几个值。结构看起来像这样,
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];
答案 0 :(得分:2)
你这样做:
// 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
。
答案 1 :(得分:-1)
你有一个字典开头,
124818240124810284.JPG //[valueForKey: 124818240124810284.JPG]
item 0 String Some Attribute 1 //[valueForKey:124818240124810284.JPG] valueForKey:Item 0];
item 1 String Some Attribute 2 //[valueForKey:124818240124810284.JPG] valueForKey:Item 1];
354273577823597297.JPG //[valueForKey: 354273577823597297.JPG]
item 0 String Some Attribute 1 //[valueForKey: 354273577823597297.JPG] valueForKey:Item 0];
item 1 String Some Attribute 2 //[valueForKey: 354273577823597297.JPG] valueForKey:Item 1;
等等......要添加它,请使用新的值/密钥对创建一个新字典:
NSMutableDictionary *newAttributes = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Attribute 0",@"Item 0",nil];
制作一个新词典:
NSMutableDictionary *newEntry = [[NSMutableDictionary alloc]initWithObjectsAndKeys:newAttributes,@"xxxxx.jpg", nil];//Makes a new dictionary
或者将数据附加到现有字典:
[dictionaryToAppend setValue:newAttribues ForKey:xxxxx.jpg"];