1

我对读/写 plist 有一些疑问。我用这个 cos 来阅读 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 中,当我删除应用程序时,这些数据会被删除吗?对?最后一个问题:当我读取文件夹中的 plist 时,我可以使用自动搜索 plist 路径的第一行代码吗?

4

1 回答 1

0

写入文件时,您做错了,因为您无法将文件写入主包。它必须写在文档目录中:

+ (NSString *) getFilePathInDocsDir: (NSString *)fileName
{
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                 NSUserDomainMask, YES);

    NSString *docsDir = documentPaths[0];
    NSString *filePathInDocsDir = [docsDir stringByAppendingPathComponent:fileName];
    return filePathInDocsDir;
}

所以使用这个函数并将文件只写入返回的路径,而不是在主包中

于 2013-02-12T09:44:08.093 回答