从plist文件写入和读取

时间:2012-04-11 00:39:06

标签: objective-c ios cocos2d-iphone plist

我目前需要读取和写入plist文件。 这是我目前的状态: 我创建了一个名为“Scores.plist”的plist文件,我把它放在我的资源文件夹中,用于我的xcode项目。 plist文件中包含一个带有“Score”键的字典。附加到键的对象是占位符值为1010的NSNumber。 当我运行我的代码时,我得到了奇怪的结果。 我的plist位于错误的地方吗? 我听说plist应该位于某些文档目录中,用于读写。但是,我不确定它到底在哪里。 任何帮助表示赞赏。提前致谢! :)

-(void)writeToPlistHighScore:(int)scoreNumber {  
    //attempt to write to plist   
    //however both the nslog's in this first section result in "null"
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Scores.plist"];

//NSString *scorePath2 = [[NSBundle mainBundle] pathForResource:@"Scores" ofType:@"plist"];
NSDictionary *plistDict = [[NSDictionary dictionaryWithContentsOfFile:filePath] retain];
NSLog(@"%@",[plistDict objectForKey:@"Score"]);
[plistDict setObject:[NSNumber numberWithInt:scoreNumber] forKey:@"Score"];
[plistDict writeToFile:filePath atomically: YES];
NSLog(@"%@",[plistDict objectForKey:@"Score"]);
[plistDict release];

//Stable code for reading out dictionary data
//this code reads out the dummy variable in Scores.plist located in my resources folder.
// Path to the plist (in the application bundle)
NSString *scorePath = [[NSBundle mainBundle] pathForResource:@"Scores" ofType:@"plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:scorePath] retain];
NSString *scoreString = [NSString stringWithFormat:@"Score:%@", [plistData objectForKey:@"Score"]];
NSLog(@"%@",scoreString);

//checking to see where my file is...
//this logs a file path which I don't know means
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory2 = [paths objectAtIndex:0];
NSString *path = [documentsDirectory2 stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",@"Scores"]]; 

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]){
    NSLog(@"File don't exists at path %@", path);

    NSString *plistPathBundle = [[NSBundle mainBundle] pathForResource:@"Scores" ofType:@"plist"];

    [fileManager copyItemAtPath:plistPathBundle toPath: path error:&error]; 
}else{
    NSLog(@"File exists at path:%@", path);
}

}

1 个答案:

答案 0 :(得分:1)

简短的回答是你想使用NSDocumentDirectory而不是NSLibraryDirectory。

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

您希望保存到其沙箱中的应用程序文档目录,而不是其应用程序包。您不允许在运行时更改应用程序包中的任何内容。

请参阅http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html以获取完整说明。

现在的诀窍是,如果你想加载一个默认的plist文件,如果还没有保存呢?好吧,首先检查你的文档目录,如果那里没有保存文件,那么从资源目录加载一个模板。更好的是,如果它看起来像一个简单的结构,只需在代码中创建NSDictionary,然后使用writeToFile:atomically:来保存它,它将以plist格式写入。

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/PropertyLists/QuickStartPlist/QuickStartPlist.html#//apple_ref/doc/uid/10000048i-CH4-SW5对plist处理有一个很好的解释。它使用Mac OS X应用程序作为示例,但这些概念可以转移到iOS。

顺便说一下,你在保留和发布时采取了一些不必要的步骤。 dictionaryWithContentsOfFile创建一个自动释放的对象,因此不需要保留它,因此在完成后无需释放它。您可以使用以下代码完成第一个代码块中正在执行的操作:

NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:filePath]; 
NSLog(@"%@",[plistDict objectForKey:@"Score"]); 
[plistDict setObject:[NSNumber numberWithInt:scoreNumber] forKey:@"Score"]; 
[plistDict writeToFile:filePath atomically: YES]; 
NSLog(@"%@",[plistDict objectForKey:@"Score"]);

当前运行循环迭代时,plistDict将自动释放。我之所以提到这一点,是因为你未能在你的样本中发布plistData,如果以后没有发布它会导致内存泄漏,而你可以通过不保留plistData来避免这种情况。有关自动释放对象的详细说明,请参阅https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html#//apple_ref/doc/uid/20000047-CJBFBEDI