0

私はいくつかの配列を持っているPlistを持っています。これらの配列内には、私が読んでいるさまざまなオブジェクトがありますが、配列内のこれらのオブジェクトを編集してから、配列をPlistファイルに再度保存することに固執しています。

私はすでにそのようにファイルを読んでいます...

Array = [myArrays objectAtIndex:arrayCounter]; //This tells it which Array to load.

myObject = [Array objectAtIndex:0]; //This reads that object in the array. (This is what I want to edit.)

そのオブジェクト(文字列)を編集して配列に保存し、その配列を保存されているPlistファイルに保存する方法を考えていました。

前もって感謝します!

PS-必要に応じて、より多くのコードや情報を投稿したいと思います。

4

1 に答える 1

1

アプリケーションのメイン バンドルにデータを保存することはできません。代わりに、次のようにドキュメント ディレクトリに保存する必要があります。

 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistFilePath = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"];

if([[NSFileManager defaultManager] fileExistsAtPAth:plistFilePath]) 
{//plist already exits in doc dir so save content in it

 NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistFilePath];
 NSArray *arrValue = [data objectAtIndex:arrayCounter];

 NSString *strValue = [arrValue objectAtIndex:0];
 strValue = [strValue stringByAppending:@"hello"]; //change your value here

 [[data objectAtIndex:arrayCounter] replaceObjectAtIndex:0 withObject:strValue]; //value replaced
 [data writeToFile:plistFilePath atomically:YES];
}
else{ //firstly take content from plist and then write file document directory. it will be executed only once

 NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
 NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath];
 NSArray *arrValue = [data objectAtIndex:arrayCounter];

 NSString *strValue = [arrValue objectAtIndex:0];
 strValue = [strValue stringByAppending:@"hello"]; //change your value here

 [[data objectAtIndex:arrayCounter] replaceObjectAtIndex:0 withObject:strValue]; //value replaced
 [data writeToFile:plistFilePath atomically:YES];

}
于 2012-09-18T12:54:32.763 に答える