трябва да можем да прочетем съдържанието на съществуващ 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];
array = [self readPlist]; array = [array arrayByAppendingObject:newEntry]; [self writePlist:array];
Това ли искате да направите? - person DarkDust   schedule 16.01.2012