2011-06-23 32 views
0

辞書の配列として初期化された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]; 

答えて

1

OK]をクリックして、裁判エラーの後、私はどのように把握これを行う。まず、保存されるデータに定義を提供しましょう。データは、毎時および非時間制のすべての軍隊の人物のデータです。 「時間データ」および「非時間データ」は、レベル1の配列と見なされます。レベル1の配列の要素は、レベル2の配列と呼ばれます。レベル2の配列は、軍隊の人が務めた国と考えることができます。レベル2の配列には、辞書である要素があります。辞書はそれぞれ、軍人を表しています。

答えは基本的には内側から作業し、保存するデータを含む辞書のレベル2の配列を作成する必要があります。次に、レベル2の配列がレベル1の配列に追加されます。最後に、レベル1の配列は、オブジェクトとして時間データまたはplistの非時間データキーに格納されます。ソリューションは次のとおりです。

// Create an array of arrays of content encoded as dictionary objects for hourly data 
armyPerson *aPerson; 
NSArray *level1Element; 
NSMutableArray *level2Array;  
NSMutableArray *level1Array = [[[NSMutableArray alloc] initWithObjects:nil] autorelease]; 
for (int i=0; i<[allHourlyPersons count]; i++) { 
    level1Element = [allHourlyPersons objectAtIndex:i]; // grab the next level1 array element. 

    // Each level1 array element will have zero or more level2Arrays. The info for each level2 dictionary needs to be collected into 
    // an level2Array element as individual dictionary objects 
    level2Array = [[[NSMutableArray alloc] initWithObjects:nil] autorelease]; // initialize the level2Array 
    for (int j=0; j<[level1Element count]; j++) { 
     aPerson = [level1Element objectAtIndex:j]; // grab the next armyPerson 

     // For each episode, create a dictionary object 
     NSDictionary *level2Element = [NSDictionary dictionaryWithObjectsAndKeys:aPerson.name, @"Name", 
                        aPerson.rank, @"Rank", 
                        aPerson.serialNo, @"serialNo", nil]; 
     [level2Array addObject:level2Element]; // save the info for this episode 
    } 

    // Now that we have all the episodes for this level1Element collected into an array of dictionary objects, 
    // we need to add this array to the level1 array 
    [level1Array addObject:level2Array]; 
} 

// Finally, we need to create the key-value pair for the hourly source array 
[data setObject:level1Array forKey:@"Hourly Data"]; 

... 
Do the same for the Non-Hourly Data prior to: 

[data writeToFile: configPlist atomically:Yes]; 
関連する問題