0

現在、plist ファイルの読み取りと書き込みが必要です。これが私の現在の状態です。「Scores.plist」という名前の plist ファイルを作成し、xcode プロジェクトの Resources フォルダーに配置しました。plist ファイル内には、「スコア」というキーを持つ辞書が含まれています。キーに添付されたオブジェクトは、プレースホルダー値が 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);
}

}

4

1 に答える 1

2

簡単に言うと、NSLibraryDirectoryの代わりにNSDocumentDirectoryを使用したいということです。

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

アプリケーションバンドルではなく、サンドボックス内のアプリケーションのドキュメントディレクトリに保存する必要があります。実行時にアプリケーションバンドルの内容を変更することは許可されていません。

詳細な説明については、 http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.htmlを参照してください。

ここでの秘訣は、デフォルトのplistファイルがまだ保存されていない場合にそれをロードしたい場合はどうなるでしょうか。まず、ドキュメントディレクトリを確認し、そこにファイルが保存されていない場合は、リソースディレクトリからテンプレートをロードします。さらに良いことに、それが見た目と同じくらい単純な構造である場合は、コードでNSDictionaryを作成し、plist形式で書き込むwriteToFile:atomically:を使用して保存します。

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/PropertyLists/QuickStartPlist/QuickStartPlist.html#//apple_ref/doc/uid/10000048i-CH4-SW5にplistの処理に関する優れた説明があります。 。例としてMacOSXアプリを使用していますが、概念は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を参照してください。

于 2012-04-11T02:51:38.203 に答える