I have some question about the reading/writing a plist.
I use this cose for read plist
NSString *strFilePath = [ [NSBundle mainBundle] pathForResource:@"miodb" ofType:@"plist" ] ;
NSMutableDictionary *dictMioDB = [[NSMutableDictionary alloc] initWithContentsOfFile:strFilePath] ;
NSString *strValue ;
strValue = [dictMioDB objectForKey: @"stringa1" ] ;
NSLog( @"%@" , strValue ) ;
And for write plist I use this code:
NSString *strFilePath = [ [NSBundle mainBundle] pathForResource:@"miodb" ofType:@"plist" ] ;
NSLog( @"%@" , strFilePath ) ;
NSMutableDictionary *dictMioDB = [[NSMutableDictionary alloc] initWithContentsOfFile:strFilePath] ;
NSString *strTextField = [NSString stringWithFormat: @"%@" , [txtField text] ] ;
// ga - salvataggio del valore nella chiave nel NSMutableDictionary
[dictMioDB setValue: strTextField forKey:@"stringa1"];
[dictMioDB writeToFile: strFilePath atomically:YES];
Now the problem is that the plist must be saved in Documents directory
So I used this code:
//percorso file su cartella documents
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *path = [documentsDir stringByAppendingPathComponent:@"iOre.sqlite"];
//[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
//controllo se il file esiste
if(![[NSFileManager defaultManager] fileExistsAtPath:path])
{
//se non esiste lo copio nella cartella cosuments
NSString *pathLocale=[[NSBundle mainBundle] pathForResource:@"iOre" ofType:@"sqlite"];
if ([[NSFileManager defaultManager] copyItemAtPath:pathLocale toPath:path error:nil] == YES)
{
NSLog(@"copia eseguita");
}
}
Now I have join the two pieces of codes in this:
//percorso file su cartella documents
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *path = [documentsDir stringByAppendingPathComponent:@"LevelsD.plist"];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
NSString *pathLocale;
//controllo se il file esiste
if(![[NSFileManager defaultManager] fileExistsAtPath:path])
{
//se non esiste lo copio nella cartella documents
pathLocale=[[NSBundle mainBundle] pathForResource:@"LevelsD" ofType:@"plist"];
if ([[NSFileManager defaultManager] copyItemAtPath:pathLocale toPath:path error:nil] == YES)
{
NSLog(@"copia eseguita");
}
}
// ga - trovo la posizione del .plist
//NSString *strFilePath = [ [NSBundle mainBundle] pathForResource:@"LevelsD" ofType:@"plist" ] ;
NSLog( @"%@" ,documentsDir) ;
// ga - copio tutto il .plist (la root è un Dictionary) in un NSMutableDictionary
NSMutableDictionary *dictMioDB = [[NSMutableDictionary alloc] initWithContentsOfFile:documentsDir] ;
NSString * txtField = @"1";
//NSString *strTextField = [NSString stringWithFormat: @"%@" , txtField ] ;
// ga - salvataggio del valore nella chiave nel NSMutableDictionary
[dictMioDB setValue: txtField forKey:@"LevelHere1"];
[dictMioDB writeToFile: pathLocale atomically:YES];
This don’t work but now I have some doubts. In my plist file I don’t see any change but is possible that value of ‘txtField’ was saved into ‘dictMioDB’? How I can see if a record/string was inserted into my plis? Another doubt: When I write something into Document folder with the simulator the data will be deleted when I close this or when I will re-compile the app? While in the Iphone this data will deleted when I remove the app? Right?
The last dubt: When I will read a plist that is in document folder I can use the firs code that search automatically the path of plist?
While writing file, you are doing wrong as you can not write file to main bundle. It must be written in documents directory:
So use this function and write the file to returned path only, not in main bundle