简体   繁体   中英

iPhone - Merge keys in a NSArray (Objective-C)

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

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM