How can I create NSDictionary NS94 but not use the same NSArray?

Windows developer for a long time, first time Objective-C / iPhone developer wants to create an NSDictionary NSArrays that can be displayed in a normal UITableView with alphabetical sections.

The source of this dictionary of arrays is an array containing store names, sorted alphabetically:

Apple Store
Atlanta Bread Company
borders displayed on the workshop construction of Bear
Gallery Cargill
Dillards
Dick Sporting Goods
Eddie Bury
chest
GameStop
Heyzberg Diamonds
LensCrafters

McDonald Nordstrom
monetary source shoe
Ceramic barn
Solstice
Coffee Starbucks
Victoria's Secret
White Barn Candle Co.

An array dictionary created from source should look something like this (data type in brackets):

Root (Dictionary)
.... A (array)
Apple Store (String)
........ Atlanta Bread Company (String)
.... B (array)
........ Boundaries ( String)
........ Construction-Bear Workshop (String)
.... C (array)
........ Cargill Gallery (String)
.... D (array)
... ..... Dillards (String)
........ Dick Sporting Goods (String)
.... etc.

A dictionary of arrays like the structure described above populates the TableView and displays as follows:

a
Apple Store
Atlanta Bread Company
B
Image Borders Construction-Bear Workshop
C
Cargill Gallery
D
Dillards
Dick Sporting Goods
etc.

The code I wrote that creates a dictionary of arrays ends up putting the last store (White Barn Candle Co.) after each section heading.

and
White Barn Candle Co.
B
White Barn Candle Co.
C
White Barn Candle Co.
D
White Barn Candle Co.
etc.

I understand this is happening because I reuse the temporary array, filling it with A, clearing it, filling it with B, clearing it, etc. Here's the actual code that creates a dictionary of arrays from the source array.

NSMutableDictionary *places = [[NSMutableDictionary alloc] init];
NSMutableArray *alphaPlace = [[NSMutableArray alloc] init]; 

for (int x = 1; x <= 26; x++) {
    NSString *alphabet = [[NSString alloc] initWithString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
    NSString *letter = [alphabet substringWithRange:NSMakeRange(x - 1, 1)];

    for (NSString *place in allPlaces) {
        NSString *first = [place substringWithRange:NSMakeRange(0, 1)];
        if ([first isEqualToString:letter] == YES) { // Add to alphaPlace
            [alphaPlace addObject:place];
        }
        else { // Add alphaPlace to places, clear out alphaPlace; add new item to alphaPlace
            [places setValue:alphaPlace forKey:letter];
            [alphaPlace removeAllObjects];  // SAME ARRAY BEING USED FOR ALL LETTERS OF ALPHABET!
            [alphaPlace addObject:place];
        }
    }
}

      

I realize I can loop over this by creating 26 different arrays and using a switch statement to pick the right one to populate, but I really don't want to. I thought that someone more familiar with Objective-C could provide me with a more elegant solution.

Thanks in advance. Let me know if you need more information.

+2


source to share


1 answer


I think you want to do something like this:



NSMutableDictionary *places = [[NSMutableDictionary alloc] init];

// initialize places with an array for each letter
NSString *alphabet = [[NSString alloc] initWithString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
for (int x = 0; x < 26; x++) {
    NSString *letter = [alphabet substringWithRange:NSMakeRange(x, 1)];
    [places setObject:[NSMutableArray array] forKey:letter];
}

// then put each place in the array corresponding to its first letter
for (NSString *place in allPlaces) {
    NSString *first = [[place substringWithRange:NSMakeRange(0, 1)] uppercaseString];
    NSMutableArray *letterArray = [places objectForKey:first];
    [letterArray addObject:place];
}

      

+11


source







All Articles