Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>[NVM, I see you got it working already! Cool.]</p> <p>Something roughly like this should work, you'll need to season and debug to taste of course. But this is the general idea...</p> <pre><code>- (NSDictionary*)categoriesAndSortedItemsFromItems:(NSArray*)source { //Given NSArray *source with the lines you described... NSMutableDictionary *categoriesAndItems = [NSMutableDictionary dictionaryWithCapacity:3]; // Plug the items into the correct arrays by category for (NSString *line in source) { NSArray *components = [line componentsSeparatedByString:@": "]; NSString *category = [components objectAtIndex:0]; //e.g. "tree" NSString *item = [components objectAtIndex:1]; //e.g. "oak" NSMutableArray *itemsForCategory = [self arrayInDict:categoriesAndItems forKey:category]; [itemsForCategory addObject:item]; } // Now run through and sort each array if you want... NSMutableDictionary *categoriesAndSortedItems = [NSMutableDictionary dictionaryWithCapacity:3]; [categoriesAndItems enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { NSArray *sorted = [(NSArray*)obj sortedArrayUsingSelector:@selector(compare:)]; [categoriesAndSortedItems setObject:sorted forKey:key]; }]; return categoriesAndSortedItems; } - (NSMutableArray*)arrayInDict:(NSMutableDictionary*)dict forKey:(NSString*)key { NSMutableArray *array = [dict objectForKey:key]; if (!array) { array = [[NSMutableArray alloc] initWithCapacity:10]; [dict setObject:array forKey:key]; [array release]; } return array; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload