Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add the object array to nsmutabledictionary with another array as key

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.

like image 999
Bala Avatar asked Jan 19 '23 05:01

Bala


2 Answers

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];
}
like image 98
Denis Avatar answered Jan 20 '23 18:01

Denis


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

like image 26
Arslan Avatar answered Jan 20 '23 20:01

Arslan