gpt4 book ai didi

ios - 为什么删除而不是添加。plist

转载 作者:行者123 更新时间:2023-11-29 03:17:24 25 4
gpt4 key购买 nike

每当我保存数据时,我都有一个代码可以将数据添加到 plist 文件对象中。他们暗恋的更早了!

除了更多我还想要它们!这怎么可能?

我保存数据的代码:

// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];

// set the variables to the values in the text fields
self.personName = nameEntered.text;
self.phoneNumbers = [[NSMutableArray alloc] initWithCapacity:3];
[phoneNumbers addObject:homePhone.text];
[phoneNumbers addObject:workPhone.text];
[phoneNumbers addObject:cellPhone.text];


// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: personName, phoneNumbers, nil] forKeys:[NSArray arrayWithObjects: @"Name", @"Phones", 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];
}

最佳答案

您每次都在用新数据覆盖 plist 文件。如果要追加,则需要先从 plist 文件加载现有数据,然后将新数据添加到该字典,然后写出更新后的字典。

关于ios - 为什么删除而不是添加。plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21532005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com