ios - Create dictionary with dictionaries of arrays (sorted by a specific custom object property)? -


the title may confusing, apologies. maybe can advise me on more appropriate title.

i have .json file structured below.

"sections": [{              "title": "locations",              "key": "location"              }], "contacts": [{              "title": "social worker",              "name": "mrs x",              "office": "xxxxxxxx",              "location": "lisburn",              "department": "xxxxxxxx",              "telephone": "xxx xxxxxxxx"              },... 

when parsing this, create array called contactsarray. can create aecontact objects array so:

    (nsdictionary *contactdic in [contactsarray valueforkey:@"contacts"]) {     // create aecontact objects     _contact = [[aecontact alloc] initwithdictionary:contactdic];     [contacts addobject:_contact]; } self.contacts = [contacts copy]; 

in self.contacts array, value contact.location property interested in. need create separate arrays of related aecontact objects based on location property, map these location key in contactarray dictionary

this have tried far:

nsmutabledictionary *locationsdic = [nsmutabledictionary new]; // loop through contacts (int = 0; < self.contacts.count; i++) {     // sort location     if ([self.contacts[i] valueforkey:@"location"] == [[[contactsarray valueforkey:@"contacts"] objectatindex:i] valueforkey:@"location"]) {         [locationsdic setvalue:self.contacts[i] forkey:[[[contactsarray valueforkey:@"contacts"] objectatindex:i] valueforkey:@"location"]];     } } 

and output is:

{     ballynahinch = "<aecontact: 0x15dda1fc0>";     bangor = "<aecontact: 0x15dda2210>";     lisburn = "<aecontact: 0x15dda1c70>";     ... } 

when aecontact object has same location, sets key/value in dictionary , overwrites previous entry. need happen this:

{     lisburn = "<aecontact: 0x15dda18f0>",               "<aecontact: 0x15dda18f0>",               "<aecontact: 0x15dda18f0>";      bangor = "<aecontact: 0x15dda18f0>",              "<aecontact: 0x15dda18f0>",              "<aecontact: 0x15dda18f0>"; } 

i'm not sure if output should should/will preview above, can assume have not yet achieved goal. how can create related aecontact objects in array , map them location key in locationsdic? thanks.

the title (and problem description) little tough follow, think you're trying index array of (aecontact) objects location parameter.

we can clarify tighter naming , find-or-create pattern process input.

nsdictionary *jsonresult = // dictionary begin nsarray *contactparams = jsonresult[@"contacts"]; nsmutabledictionary *contactsbylocation = [@{} mutablecopy];  (nsdictionary *contactparam in contactparams) {     nsstring *location = contactparam[@"location"];      // here's important part: find array of aecontacts in our indexed result, or      // create if don't find     nsmutablearray *aecontacts = contactsbylocation[location];     if (!aecontacts) {         aecontacts = [@[] mutablecopy];         contactsbylocation[location] = aecontacts;     }     aecontact *aecontact = [[aecontact alloc] initwithdictionary:contactparam];     [aecontacts addobject:aecontact]; } 

contactsbylocation think you're looking for.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -