4
for(Attribute* attribute in appDelegate.attributeArray) {
    attribute = [appDelegate.attributeArray objectAtIndex:z];
    attri = attribute.zName;
    int y = 0;
    for(Row* r in appDelegate.elementsArray) {
        r = [appDelegate.elementsArray objectAtIndex:y];
        NSString *ele = r.language;
        if([attri isEqualToString:ele]) {
            NSLog(@"=================+++++++++++++++++%@ %@",attri, r.user);
            [aaa insertObject:r atIndex:y]; //here i am adding the value to array
            [dict setObject:aaa forKey:attri]; //here i am adding the array to dictionary
        }
        y++;
    }
    z++;
    NSLog(@"============$$$$$$$$$$$$$$$$$$$$$$$++++++++++ %@",dict);
}

key in one array and the value in the another array and the value array is in object format.

I need to store the multi object for the single key. The attributeArray has the key value and the elementsArray has the object. For example the attributeArray might have the values

 " English, French, German..."

and the elementsArray might have the object value

"<Row: 0x4b29d40>, <Row: 0x4b497a0>, <Row: 0x4e38940>, <Row: 0x4b2a070>, <Row: 0x4b29ab0>, <Row: 0x4b178a0> "

In the first value I need to store the two object and for second key I need to store 3 objects and for the third key in need to store last two objects in the dictionary.

zaph
  • 111,848
  • 21
  • 189
  • 228
Bala
  • 136
  • 1
  • 1
  • 14
  • here i need to store the multi object for the single key.here the attributeArray having the key value and the elementsArray having the object.for example the attributeArray having the values " English, French, Germany..."and the elementsArray having the object value ", , , , , " in the first value i need to store the two object and for second key i need to store 3 objects and for the 3 key in need to store last 2 object in dictionary. – Bala Nov 23 '11 at 09:31

2 Answers2

6

For super-simplification you can use the following code:

NSArray *keys = ...;
NSArray *values = ...;

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjects: values forKeys: keys];

Hope, this helps.

UPDATE:

to store multiple values for single key in the dictionary, just use NSArray / NSMutableArray as your object:

NSArray *keys = ...;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];

for( id theKey in keys) 
{
  NSMutableArray *item = [NSMutableArray array];
  [item addObject: ...];
  [item addObject: ...];
  ...

  [dict setObject: item forKey: theKey];
}

If you don't know all the values for the key from the beginning and need to add them one by one, you can use the following approach:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

for( /*some cycling condition here */) 
{
  id keyToUse = ...;
  id valueToAdd = ...;

  id foundArray = [dict objectForKey: keyToUse];
  if ( nil == foundArray )
  {
    foundArray = [NSMutableArray array];
    [dict setObject: foundArray forKey: keyToUse];
  }

  [foundArray addObject: valueToAdd];
}
Denis
  • 6,313
  • 1
  • 28
  • 27
  • thanks denis i think u hav partially understood my question if not i hav explain it to verbia. if possible can u check it. – Bala Nov 23 '11 at 09:50
  • first i am parsing the file testxml file and storing it in two array and trying to make the arrays as a dictionary to show in the table view.In table view header is the attribute value in the userlanguage and the tableviewcell should display the username and the url and the cdate. but i cant to make the dictionary. – Bala Nov 23 '11 at 11:54
  • the reason you code crashes with Out-of-bounds exception is because you are not resetting e local var back to zero before the line "for( Row* r in appDelegate.elementsArray) {id valueToAdd = [appDelegate.elementsArray objectAtIndex:e];" in XMLParser.m; after that you will see a crash on the line "id keyToUse = [appDelegate.headerArray objectAtIndex:d];", because headerArray & elementsArray have different count; also, i can't understand, why are you using index access in for-each cycle-statements – Denis Nov 23 '11 at 13:55
  • getting the values of the object stored in the elementarray.is there any other way to access the values. – Bala Nov 24 '11 at 05:06
  • you can iterate array items like the following: for( id theKey in keys) { NSLog(theKey);} in every iteration of the array, theKey will be next item in array – Denis Nov 24 '11 at 09:13
  • thanks Denis through looping i got wat i need. shall i contact u thru mail for further. – Bala Nov 24 '11 at 11:24
  • you can post your questions here =) – Denis Nov 24 '11 at 13:58
3

To me it looks you are settings an array (aaa) with string (attri) as a key.

To set an array as a key for another array as an object. You can do this with the following appraoch.

NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithCapacity:1];

NSArray *valueArray = [NSArray arrayWithObjects:@"v1", @"v2", nil];
NSArray *keyArray = [NSArray arrayWithObjects:@"k1",@"k2", nil];

[myDictionary setObject:valueArray forKey:keyArray];
NSLog(@"myDictionary: %@", myDictionary);

But you should review your code and provide a more brief explanation that what do you want to achieve and how are you taking an approach for this.

regards,

Arslan

Arslan
  • 919
  • 7
  • 16
  • this is not a good solution - you have an array instance as key :/ – Denis Nov 23 '11 at 08:07
  • @Denis . I know its not a good solutions but i think this is what the question was asked for. Bala has explained the question and now you can see that my answer was appropriate in the light of explanation the user has provided becuase the values and keys array can have different length. But if we are going to use "dictionaryWithObjects: values forKeys: keys" then we will have to make sure that both arrays have same length which the user is actually not looking for. I hope I have clear my point. Thanks. – Arslan Nov 24 '11 at 07:43
  • my point was it's not a good solution, to use collection instance as key, nothing personal – Denis Nov 24 '11 at 11:28