нам нужно иметь возможность читать содержимое существующего файла 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