问题
I am writing these data to plist file, but every time I write, it replaces whatever was there before. I.e it is overwriting whatever data I had previously when I reload the page. How can I make the previous data persist or could someone point out what I am not doing correctly?
- (IBAction)buttonClickSave:(id)sender;
{
pathList=[self getFilePath];
if(![[NSFileManager defaultManager] fileExistsAtPath:pathList])
{
[[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle] pathForResource:@"adresses" ofType:@"plist" ]toPath:pathList error:nil];
}
NSString *Name = @"Names";
NSString *email = @"email";
NSString *phone = @"phone";
NSMutableArray *values = [[NSMutableArray alloc] initWithObjects:Name, email, phone, nil];
NSMutableArray *keys = [[NSMutableArray alloc] initWithObjects:kTitleKey, kEmail, kPhone, nil];
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
[self.arraysData addObject:dict];
[self.arraysData writeToFile:pathList atomically:YES];
}
回答1:
To initialize the array, use the existing file as its source. This will ensure that whatever entries you are adding is not overwriting but making changes to existing data.
So, wherever you are initializing the arraysData
, use following code:
arraysData = [NSMutableArray arrayWithContentsOfFile: [self getFilePath]];
回答2:
All you have to do is read all data from plist, then append your data and writes this data into plist
To read all the data
NSMutableArray *plistArray = [NSMutableArray arrayWithContentsOfFile:pathList];
To append your data
[plistArray addObject:YOURDATA];
Then write your array like this
[dict writeToFile:plistpath atomically:YES];
回答3:
Before insert you get all Data value in plist. after compare the data with you logic or example one use to insert data with out overwrite
// get plist paths from root directory
NSArray *plistPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPathPlist = [paths objectAtIndex:0];
NSString *plistDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0]; // get the path to our plist file
NSLog(plistDirectory);
NSString *plistPathRoot = [plistDirectory stringByAppendingPathComponent:@"Sample.plist"];
//This copies objects of plist to array if there is one
[array addObjectsFromArray:[NSArray arrayWithContentsOfFile:plistPathRoot]];
//If plist has less than 10 items, insert the new data. Don't use lastInsertionIndex. Else, replace one of the items.
if([array count] < 10)
{
[array addObject:[textName.text]];
}
else
{
//Update the lastInsertionIndex
lastInsertionIndex++;
lastInsertionIndex %= 10; //// array size = 10
[array replaceObjectAtIndex:lastInsertionIndex withObject:[textName.text]];
}
[array writeToFile:plistPath atomically: TRUE];
Another:
this below link has explain vastly about Plist Without Overwrite with example project too.
how-to-stop-overwriting-data-ios
来源:https://stackoverflow.com/questions/20291086/saving-ios-data-to-plist