我对读/写plist有一些疑问。 我用这个cose读取plist
NSString *strFilePath = [ [NSBundle mainBundle] pathForResource:@"miodb" ofType:@"plist" ] ;
NSMutableDictionary *dictMioDB = [[NSMutableDictionary alloc] initWithContentsOfFile:strFilePath] ;
NSString *strValue ;
strValue = [dictMioDB objectForKey: @"stringa1" ] ;
NSLog( @"%@" , strValue ) ;
对于写plist我使用此代码:
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];
现在的问题是plist必须保存在Documents目录中 所以我使用了这段代码:
//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");
}
}
现在我加入了两段代码:
//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];
这不行,但现在我有些疑惑。在我的plist文件中,我没有看到任何变化,但'txtField'的值可能保存到'dictMioDB'中?如何在我的plis中插入记录/字符串?另一个疑问:当我使用模拟器在Document文件夹中写入内容时,当我关闭它或者我将重新编译应用程序时,数据将被删除?在Iphone中,当我删除应用程序时,这些数据会被删除吗?对? 最后一个dubt:当我读到文件夹中的一个plist时,我可以使用第一个自动搜索plist路径的代码吗?
答案 0 :(得分:0)
在编写文件时,由于无法将文件写入主包,因此操作错误。它必须写在文档目录中:
+ (NSString *) getFilePathInDocsDir: (NSString *)fileName
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *docsDir = documentPaths[0];
NSString *filePathInDocsDir = [docsDir stringByAppendingPathComponent:fileName];
return filePathInDocsDir;
}
因此,请使用此函数并将文件写入返回的路径,而不是在主程序包
中