So I have an NSDictionary where one of the keys is an array of dictionaries. The class I'm mapping to has matching key names and setters. Can setValuesForKeysWithDictionary fill the sub-dictionaries for me? When I tried it, it seemed like it filled the objects with pointer junk or something, but I'm a newbe, so maybe I'm doing something wrong. Does that function work like that?
1 Answers
I realized that there was no way for setValuesForKeysWithDictionary to know what kind of object to fill the NSMutableArray with. I ended up making a custom setter for the array property that manually loops the elements of the array (of NSDictionaries) you pass in and calls setValuesForKeysWithDictionary for each one.
Here's the code:
There is a property called itemList of type NSMutableArray that I want filled it objects of type Item. The setItemList setter loops through the array of mystery objects, converting each NSDictionary to my Item type and adds them to a new array. Any comments on how to simplify the code would be welcome.
I also want to add some logic here to handle a situation where the array already contains Item objects instead of dictionaries. In actionscript you can check for null after you try to cast something to see if it worked, not sure what the equivalent process would be here. [item isMemberOfClass [Item class]]
always evaluates to YES, even if item is an NSDictionary. I can't understand why...
- (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;
}

- 1,475
- 14
- 29