You can use setValuesForKeysWithDictionary with a nested dictionary

So, I have an NSDictionary where one of the keys is a set of dictionaries. The class I'm attached to has corresponding key names and setters. Can setValuesForKeysWithDictionary fill in sub-dictionaries for me? When I tried this, it seemed like he filled the objects with a garbage pointer or something like that, but I'm new to it, so maybe I'm doing something wrong. Does this function work like this?

+3
source share
1 answer

I realized that there is no way for setValuesForKeysWithDictionary to know which object the NSMutableArray should fill. I ended up creating a custom parameter for an array property that manually bypasses the array elements (NSDictionaries) that you pass in and calls setValuesForKeysWithDictionary for each of them.

Here is the code:

There is a property called itemList of type NSMutableArray that I want to populate for it with objects of type Item. SetItemList setter traverses an array of mystery objects, converting each NSDictionary to my Item type and adding them to a new array. Any comments on how to simplify the code are welcome.

, , Item . actionscript null , - , , , , . [item isMemberOfClass [Item class]] YES, NSDictionary. , ...

 - (void) setItemList:(NSMutableArray*)input{
        [itemList autorelease];
        itemList = [[NSMutableArray alloc] initWithCapacity:input.count];

        //loop through the array, creating an Item for for each object in the array
        for(int i=0;i<input.count;i++){


            Item* item = [Item new];
            [item setValuesForKeysWithDictionary:(NSDictionary*)[input objectAtIndex:i]]; 
            [itemList insertObject:item atIndex:i];

        }
    }
    - (NSMutableArray*) itemList{
        return itemList;
    }
+4

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


All Articles