How to add an array of objects to nsmutabledictionary with another array as a 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); } 

in one array and the value in another array, and the array of values ​​in the object format.

I need to save a multi-object for one key. attributeArray is the key value, and elementsArray is the object. For example, an Array attribute may have values

  " English, French, German..." 

and elementArray can have an object value

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

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

+4
source share
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 a single key in a dictionary, just use NSArray / NSMutableArray as an 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 do not know all the values ​​for the key from the very beginning and you 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]; } 
+6
source

It seems to me that you are the array settings (aaa) with the string (attri) as the key.

Set the array as a key for another array as an object. You can do this with the following rating.

 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 look at your code and give a shorter explanation of what you want to achieve and how you take an approach to this.

Yours faithfully,

Arslan

+3
source

Source: https://habr.com/ru/post/1382644/


All Articles