views:

391

answers:

1

Hi everyone, I'm having troubles with arrays and keys... I have an array from my database:

NSArray *elementArray = [[[menuArray valueForKey:@"meals"] valueForKey:@"recipe"] valueForKey:@"elements"]

The problem here is that I would like all my elements of all my meals of all my menus in an array such that: [elementArray objectAtIndex:0] = my first element etc... In the example above, the elements are separated by the keys. How can I get that?

Hope it's clear enough...

Thanks

A: 

From your code snippet, it is not clear to me exactly how your data is structured, but I think it's analogous to having an NSDictionary (called aDictionary) of NSArray and wanting to combine all the NSArray into one. If this is the case, then:

NSMutableArray *resultArray = [[[NSMutableArray alloc] init] autorelease];
for (id dictionaryKey in aDictionary) {
    [resultArray addObjectsFromArray:[aDictionary objectForKey:dictionaryKey]];
}
return [NSArray arrayWithArray:resultArray];

(This code has not been tested.)

Isaac
It doesn't seem to be what I'm looking for... but thanks! Look at my comment (of my own question)
ncohen
Are you using key-value coding and/or CoreData?
Isaac
Yes, I'm using core data. The problem is when I get an array of objects (such as my three menus) [user.menu allObjects], I can reach its properties (such as meal, recipe and element) with keys [[user.menu allObjects] valueForKey:@"meal"]. But like I said above, I would like all my elements in an array
ncohen