Search code examples
iosobjective-cjsonnsarraynsmutabledictionary

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


The title may be confusing, apologies. Maybe someone can advise me on a more appropriate title.

I have a .json file structured as 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, I create an array called contactsArray. I can then create AEContact objects from this array like so:

    for (NSDictionary *contactDic in [contactsArray valueForKey:@"contacts"]) {
    // Create AEContact objects
    _contact = [[AEContact alloc] initWithDictionary:contactDic];
    [contacts addObject:_contact];
}
self.contacts = [contacts copy];

In the self.contacts array, the value for the contact.location property is what I am interested in. I need to create separate arrays of related AEContact objects based on the location property, then map these to the location key in my contactArray dictionary

This is what I have tried so far:

NSMutableDictionary *locationsDic = [NSMutableDictionary new];
// Loop through contacts
for (int i = 0; i < self.contacts.count; i++) {
    // Sort by 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 the output is:

{
    Ballynahinch = "<AEContact: 0x15dda1fc0>";
    Bangor = "<AEContact: 0x15dda2210>";
    Lisburn = "<AEContact: 0x15dda1c70>";
    ...
}

When an AEContact object has the same location, it sets it as another key/value in the dictionary and overwrites the previous entry. What I need to happen is something like this:

{
    Lisburn = "<AEContact: 0x15dda18f0>",
              "<AEContact: 0x15dda18f0>",
              "<AEContact: 0x15dda18f0>";

    Bangor = "<AEContact: 0x15dda18f0>",
             "<AEContact: 0x15dda18f0>",
             "<AEContact: 0x15dda18f0>";
}

I'm not sure if the output should should/will look like the preview above, I can only assume as I have not yet achieved my goal. How can I create the related AEContact objects in an array and map them to location key in my locationsDic? Thanks.


Solution

  • The title (and the problem description) are a little tough to follow, but I think you're trying to index an array of (AEContact) objects by their location parameter.

    We can clarify this just with some tighter naming and with a find-or-create pattern as we process the input.

    NSDictionary *jsonResult = // the dictionary you begin with
    NSArray *contactParams = jsonResult[@"contacts"];
    NSMutableDictionary *contactsByLocation = [@{} mutableCopy];
    
    for (NSDictionary *contactParam in contactParams) {
        NSString *location = contactParam[@"location"];
    
        // here's the important part: find the array of aecontacts in our indexed result, or 
        // create it if we don't find it
        NSMutableArray *aeContacts = contactsByLocation[location];
        if (!aeContacts) {
            aeContacts = [@[] mutableCopy];
            contactsByLocation[location] = aeContacts;
        }
        AEContact *aeContact = [[AEContact alloc] initWithDictionary:contactParam];
        [aeContacts addObject:aeContact];
    }
    

    contactsByLocation will be what I think you're looking for.