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.

1
  • 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 "<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 3 key in need to store last 2 object in dictionary. Commented Nov 23, 2011 at 9:31

2 Answers 2

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];
}
Sign up to request clarification or add additional context in comments.

7 Comments

thanks denis i think u hav partially understood my question if not i hav explain it to verbia. if possible can u check it.
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.
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
getting the values of the object stored in the elementarray.is there any other way to access the values.
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
|
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

3 Comments

this is not a good solution - you have an array instance as key :/
@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.
my point was it's not a good solution, to use collection instance as key, nothing personal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.