0

私は単純なショッピング リスト プログラムを作成しようとしましたが、変更された値を plist ファイルに書き戻そうとすると、最初にファイルにあった元の値しか取得できません。

私の元の plist ファイルは、辞書の 1 つの大きな配列 (dataArray) であり、各辞書には 1 つの bool と 1 つの文字列の 2 つのフィールドしかありません。コンソール 値が変更されたことがわかります。私の唯一の問題は、dataArray をファイルに書き込もうとすると、ドキュメント ディレクトリにあるファイルを取得することですが、すべての bool 値がまだオフになっていることです。

- (void)viewDidLoad {

 //load from plist file
 NSString *path = [[NSBundle mainBundle] pathForResource:@"OriginalChecklist" ofType:@"plist"];
 self.dataArray = [NSMutableArray arrayWithContentsOfFile:path];

    [super viewDidLoad];
}

- (void)viewWillDisappear:(BOOL)animated 
{
 NSString *documentsDirectory = [@"~/Documents/" stringByExpandingTildeInPath];
 NSString *savePath = [documentsDirectory stringByAppendingPathComponent:@"Checklist.plist"];
 [dataArray writeToFile:savePath atomically:YES];

 NSLog(@"location information %@", savePath); 
 NSLog(@"dataAray contents %@=", dataArray);
}

そして、dataArray の結果をコンソールに出力したときに言ったように、チェックした項目から次の結果が得られます。

cell = <UITableViewCell: 0x3b143d0; frame = (0 0; 320 44); text = 'Water Bottle'; autoresize = W; layer = <CALayer: 0x3b14650>>;
checked = 1;
text = "Water Bottle";

cell = <UITableViewCell: 0x3b20560; frame = (0 88; 320 44); text = 'GPS'; autoresize = W; layer = <CALayer: 0x3b20610>>;
checked = 1;
text = GPS;

変更された結果を plist ファイルに保存する方法を誰かが知っていれば、少し助けていただければ幸いです。

ありがとうブラッド


アプリケーションが plist を作成し、ViewDidLoad セクションでその plist から情報をロードできることに、まだ少し問題があります。元のファイルを確認するために現在使用しているコードは、次のとおりです。

(void)viewDidLoad {

BOOL success;

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"CustomChecklist.plist"];

success = [fileManager fileExistsAtPath:filePath];
NSLog(@"STATUS OF SUCCESS %@",success);
if (!success) {
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"OriginalChecklist.plist"];
    success = [fileManager copyItemAtPath:path toPath:filePath];
    self.dataArray = [NSMutableArray arrayWithContentsOfFile:filePath];
    NSLog(@"IF STATEMENT CREATING THE FILE");
}else {
    self.dataArray = [NSMutableArray arrayWithContentsOfFile:filePath];
    NSLog(@"IF STATEMENT READING THE FILE");
}

NSLog(@"location information %@", filePath);
[super viewDidLoad];

}

このアプリケーションをビルドすると、正常にビルドおよび実行されますが、いくつかの警告が表示されます NSFileManager may not Respond to -copyItemAtPath:toPath

これが完了すると、コンソールへの出力は、ファイルをチェックしたり、読み取るファイルを作成したりしていないように見えます。

2010-07-06 16:22:42.803 CampingChecklist[400:207] 成功のステータス (null) 2010-07-06 16:22:42.805 CampingChecklist[400:207] ファイル 2010-07-06 16 を読み取る IF ステートメント: 22:42.814 CampingChecklist[400:207] 位置情報 /Users/Brad/Library/Application Support/iPhone Simulator/3.1.3/Applications/E7074D5A-3E7A-4D2E-9F92-82B1A04873F8/Documents/CustomChecklist.plist

そして明らかに、ファイルには何もないため、アプリケーションの途中で保存しようとすると、指定されたパスにファイルが作成されません。

誰かがこの問題に光を当てることができれば、私はそれを高く評価します.

ありがとうブラッド

4

1 に答える 1

3

plistを初めてロードするときは、AppBundleから取得されます。これは書き込み可能ではありません。

データを保存すると、ドキュメントディレクトリに正しく保存されます。

ただし、次回plistを再度ロードするときは、変更されていないAppeバンドルから再度ロードします。

ドキュメントディレクトリにファイルが存在するかどうかをチェックするロジックをplistロードコードに組み込む必要があります。存在する場合はロードし、存在しない場合はアプリバンドルからロードします。

于 2010-06-21T15:07:26.707 に答える