¿Cómo agregar datos en plist? [duplicar]
Frecuentes
Visto 674 veces
0
I have a .plist file. When I write data to plist it does't append but overwrites the previous content.
Aquí está mi código
// 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:@"HistoryList.plist"];
// set the variables to the values in the text fields
NSString *str=[NSString stringWithFormat:@" %@ %@ %@",strname.text,strnum.text,selectfield.text];
self.historyArray = str;
// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: historyArray, nil] forKeys:[NSArray arrayWithObjects: @"Name", 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);
}
How to write data in plist such that it gets appended to previous and does not overwrite?
1 Respuestas
1
- Read the .plist file and load as dictionary
- modify the dictionary with your new items
- save the complete dictionary replacing the old file
respondido 27 nov., 13:07
but how can i do it ?? - user3040536
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: plistPath]; - Traze
how to perform with swift - Karthi Rasu
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas ios iphone objective-c plist or haz tu propia pregunta.
stackoverflow.com/questions/8884215/… - the1pawan
@Bhumeshwerkatre So close the question rather than just making a comment. - trojanfoe
@user3040536 ipgames.wordpress.com/tutorials/writeread-data-to-plist-file, check the above link may be it will helps you. - Himanshu padia