0

NSDictionaries の配列があります。現在、この配列には、簡単にするために 2 つの辞書のみが含まれています。

コードをステップ実行していると、それぞれNSDictionaryが適切に作成され、期待される値が含まれています...次の方法でこの配列 (モデルに入力されています) にアクセスします。

@property (nonatomic, retain) NSMutableArray *sDictionaries;

コントローラーでこのプロパティにアクセスするには、次の方法を使用します。

NSArray *array = [[NSArray alloc]initWithArray:sLocator.sDictionaries]しかし、内部を見るarrayと、キーのみが含まれており、値は含まれていません(モデルで辞書が作成されたときに既に見たものです)。

NSDictionaryこのオブジェクトの配列をモデルからコントローラーに渡す際の助けをいただければ幸いです。

編集:これは、受信したデータを反復処理してNSDictionaryオブジェクトに配置するコードです。

NSMutableArray *arrayOfObjects = [[NSMutableArray alloc]initWithCapacity:0];
sDictionaries = [[NSMutableArray alloc]initWithCapacity:0];

int i = 0;
//iterate through each element, get the string, then add to the array
//I know for this example I am receiving 10 element objects which are NSStrings. 
//The first string is the key of the dictionary and the next 4 strings are the values.
//Then all the objects are removed from arrayOfObjects and a new dictionary is made
for(Element *element in nodes)
{
    [mArray addObject:[element content]];

    if(i%5 != 0)
    {
        [arrayOfObjects addObject:[element content]];
    }

    if(i%5 == 0)
    {   
        [idArray addObject:[element content]];
    }

    if([arrayOfObjects count] >= 4)
    {
       //Here is where the dictionary is created then added to the array. 
        NSDictionary *dictionary = [NSDictionary dictionaryWithObject:arrayOfObjects forKey:[idArray lastObject]];
        [sDictionaries addObject:dictionary];

        [arrayOfObjects removeAllObjects];
    }

    i++;
}
4

1 に答える 1

2

この線は最悪だと思います。 NSDictionary *dictionary = [NSDictionary dictionaryWithObject:arrayOfObjects forKey:[idArray lastObject]];

allObjectsをremoveAllObjectsディクショナリに追加した後で削除したため、オブジェクト自体が削除されます。その行をに変更してみてください

NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[NSArray arrayWithArray:arrayOfObjects] forKey:[idArray lastObject]];

于 2013-02-19T00:26:10.410 に答える