辞書の配列として初期化されたplistを作成しました。 plistは動的データを格納します。したがって、アプリケーションが終了すると、追加する新しい辞書要素が存在する可能性があります。または、追加する辞書要素の新しい配列が存在する可能性があります。後で、辞書要素が少なくなり、前の要素がもはや必要なくなる。 plistに要素を追加することについては多くの議論がありますが、私の問題には対処できないようです。ここでは初期のplistの説明は次のとおりです。辞書の配列を含むplistで新しい辞書要素を追加して書き出す方法
Key Type Value
Root Dictionary (2 items)
Hourly Data Array (1 item)
Item 0 Array (1 item) <--how do I add more of these
Item 0 Dictionary (3 items) <--how do I add more of these
Name String Joe
Rank String private
serialNo String 123456
NonHourly Data Array (1 item)
Item 0 Array (1 item)
Item 0 Dictionary (3 items)
Name String Jeff
Rank String private
serialNo String 654321
私は完全にこのplistファイルを読み込む方法を理解しますが、私は時間単位とNonHourlyデータアレイに配列要素を追加する方法を理解していないか、または新しい辞書の配列を追加する方法アイテムを作成し、plistに書き戻します。
それでは、どのように私は配列要素と、上記の辞書の要素追加するには、このコードを完了しない、出発点として、次のコードを与えられた:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // Create a list of paths
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *configPlist = [documentsDirectory stringByAppendingPathComponent:@"config.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: configPlist];
...
[data writeToFile: configPlist atomically:YES];