2012-04-11 28 views
0

現在、plistファイルの読み書きが必要です。 ここに私の現在の状態があります: 私は "Scores.plist"というplistファイルを作成しました。私はxcodeプロジェクト用のResourcesフォルダに入れました。 plistファイルの中には、キー "Score"を持つ辞書が含まれています。キーに添付されたオブジェクトは、プレースホルダ値が1010のNSNumberです。 コードを実行すると、私は奇妙な結果を得ます。 私のplistは間違った場所にありますか? 私は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

短い答えは、あなたがNSDocumentDirectory代わりのNSLibraryDirectoryを使用したいということです。

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

アプリケーションバンドルではなく、サンドボックス内のアプリケーションのドキュメントディレクトリに保存します。実行時にアプリケーションバンドルの内容を変更することはできません。

http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.htmlを参照してください。

ここで、デフォルトのplistファイルがまだ保存されていない場合は、読み込むことができますか?さて、まずドキュメントディレクトリをチェックしてください。そこにファイルが保存されていなければ、resourcesディレクトリからテンプレートをロードしてください。さらに、単純な構造の場合は、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に移行可能です。

btw、あなたはあなたの保持とリリースでいくつかの不必要な手順を取っています。 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を保持していないだけで回避できるのに対し、後でリリースされない場合はメモリリークです。自動リリースされたオブジェクトの詳細については、https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html#//apple_ref/doc/uid/20000047-CJBFBEDIを参照してください。

関連する問題