I have this piece of code for placing elements from an array into the appropriate section in a UITableView depending on their starting letter:
//For each product in the appDelgates products
for (Product *product in appDelegate.m_Products){
if ([product.category isEqualToString:productType]){
//firstLetter is equal to the first letter of the products name
NSString * l_FirstLetter = [product.name substringToIndex:1];
//convert firstString to uppercase
l_FirstLetter = [l_FirstLetter uppercaseString];
//Check what letter firstString is equal to in the alphabet
//Place in appropriate section
//If none, place in "Other" category
if ([l_FirstLetter isEqualToString:@"A"]) {
[[m_AlphabetDictionary objectAtIndex:0] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"B"]) {
[[m_AlphabetDictionary objectAtIndex:1] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"C"]) {
[[m_AlphabetDictionary objectAtIndex:2] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"D"]) {
[[m_AlphabetDictionary objectAtIndex:3] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"E"]) {
[[m_AlphabetDictionary objectAtIndex:4] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"F"]) {
[[m_AlphabetDictionary objectAtIndex:5] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"G"]) {
[[m_AlphabetDictionary objectAtIndex:6] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"H"]) {
[[m_AlphabetDictionary objectAtIndex:7] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"I"]) {
[[m_AlphabetDictionary objectAtIndex:8] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"J"]) {
[[m_AlphabetDictionary objectAtIndex:9] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"K"]) {
[[m_AlphabetDictionary objectAtIndex:10] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"L"]) {
[[m_AlphabetDictionary objectAtIndex:11] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"M"]) {
[[m_AlphabetDictionary objectAtIndex:12] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"N"]) {
[[m_AlphabetDictionary objectAtIndex:13] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"O"]) {
[[m_AlphabetDictionary objectAtIndex:14] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"P"]) {
[[m_AlphabetDictionary objectAtIndex:15] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"Q"]) {
[[m_AlphabetDictionary objectAtIndex:16] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"R"]) {
[[m_AlphabetDictionary objectAtIndex:17] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"S"]) {
[[m_AlphabetDictionary objectAtIndex:18] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"T"]) {
[[m_AlphabetDictionary objectAtIndex:19] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"U"]) {
[[m_AlphabetDictionary objectAtIndex:20] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"V"]) {
[[m_AlphabetDictionary objectAtIndex:21] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"W"]) {
[[m_AlphabetDictionary objectAtIndex:22] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"X"]) {
[[m_AlphabetDictionary objectAtIndex:23] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"Y"]) {
[[m_AlphabetDictionary objectAtIndex:24] addObject:product];
}
else if ([l_FirstLetter isEqualToString:@"Z"]) {
[[m_AlphabetDictionary objectAtIndex:25] addObject:product];
}
else {
[[m_AlphabetDictionary objectAtIndex:26] addObject:product];
}
}
}
Is there a better approach?
Thanks,
Jack
transform the char to an int minus the int of a char of a “A”
and just use
[[m_AlphabetDictionary objectAtIndex:myInt] addObject:product];and make sure, you are only using letters. and If you want to support small and big letters, you have to subtract char of ‘a’ from small letters.
there are more sophisticated approaches if you need to support locales and diacritics in the future, but this will work for the a…z and A…Z sets defined in the OP:
First, the wordy version which documents how this works:
Now the short version, which is more along the lines of what you may see in the wild:
In use:
Finally, the short in-line implementation could take this form:
please also note my ObjC/Block-style answer