阅读/写作plist
问题描述:
我对阅读/写作plist有一些疑问。 我用这个COSE读的plist阅读/写作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中必须保存在文档目录 所以我用这个代码:
//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中?另一个疑问:当我用模拟器写入文档文件夹的时候,当我关闭它或者我将重新编译应用程序时,数据将被删除?在iPhone中删除应用程序时,这些数据会被删除吗?对? 最后一个dubt:当我将读取一个位于文档文件夹中的plist时,我可以使用自动搜索plist路径的第一个代码?
答
写入文件时,由于无法将文件写入主包,因此出错。它必须写在文件目录:
+ (NSString *) getFilePathInDocsDir: (NSString *)fileName
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *docsDir = documentPaths[0];
NSString *filePathInDocsDir = [docsDir stringByAppendingPathComponent:fileName];
return filePathInDocsDir;
}
所以使用这个功能,将文件写入返回的路径而已,不是在主束