2

Xcodeで.plistファイルを手動で作成してから、定数データ(小さなデータベースの一種)と、それぞれが文字列と数値を持ついくつかのオブジェクトを追加する必要があります。次に、プログラムが起動するたびに、プログラムでそれを配列に読み込みます。.plistファイルは変更されません。.plistを作成し、手動でデータを入力する方法が見つかりません。

4

2 に答える 2

8

とても簡単です。変更することはないので、file-> new-> resource-> plist ..として追加できます。次に、データを好きなように手動で入力します。

plistの読み取りは、次のように実行できます。

NSURL *file = [[NSBundle mainBundle] URLForResource:@"myplist" withExtension:@"plist"]; //Lets get the file location

 NSDictionary *plistContent = [NSDictionary dictionaryWithContentsOfURL:file];

そして、plist内のものへのアクセスは次のようになります:

NSString *playerName = [plistContent objectForKey@"player"];

xcodeplistエディターでキー名を設定します。これは読み取りでのみ機能することに注意してください。plistに書き込むには、アプリケーションのドキュメントディレクトリにコピーする必要があります。必要に応じて投稿することもできます。

于 2012-12-04T18:23:13.930 に答える
0

NSMutableDictionaryを使用し、それにNSMutableArrayを子として指定してから、writeToFileを呼び出します。

実用的なサンプルコード:

    NSMutableArray *myArrayToWrite = [NSMutableArray array];
    [myArrayToWrite addObject:@"blablub"];
    [myArrayToWrite addObject:[NSNumber numberWithInt:123]];

    NSMutableDictionary *plistToWrite = [NSMutableDictionary dictionary];
    [plistToWrite setObject:myArrayToWrite forKey:@"data"]; 

    [plistToWrite writeToFile:@"/Users/Shared/TEMP.plist" atomically:NO];

    //---
    NSDictionary *plistRead = [NSDictionary dictionaryWithContentsOfFile:@"/Users/Shared/TEMP.plist"];

    NSArray *arrayRead = [plistRead objectForKey:@"data"];
    NSLog(@"%@", arrayRead);
于 2012-12-04T18:28:13.067 に答える