views:

26

answers:

1

Hi all,

I need to parse an XML file so that i can use and indexed UITableView. If I parse the xml, i create an array that holds Companys (for example). I have a class that is the Company, with it's address, etc... How can I go from one array to an array of arrays, with the A, B, C, ...? Do i must create so many arrays has the letters of the alphabet? Is there an easier way?

Thanks,

rl

A: 

You can filtered your array dynamically, but for performance it should be better to filter data before. You can have an array of array (I should prefer a dictionary of array).

PS: You can tag your post with iphone or ios

Create the dictionary:

NSMutableDictionary* sectionDict = [[NSMutableDictionary alloc] initWithCapacity:26];

For each element 'company' which I suppose have a 'name' NSString field:

{
  NSString *firstLetter = [[company.name substringToIndex:0] uppercaseString];
  if ([sectionDict objectForKey:firstLetter]==nil) {
    [sectionDict setObject:[NSMutableArray array]];
  } 
  [[sectionDict objectForKey:firstLetter] addObject:company];
}
Benoît
you mean, a dictionary of arrays, right? But, when i'm parsing the xml from a server, i need to check to what letter the Company starts with, to add to the correspondent array, correct?
Rui Lopes
What is your problem ? You don't know how to parse your xml file, or you don't know how to create the dictionary of array with the first letter ?
Benoît
Parse the XML - no problem. What i'm confused about is how to add each Company to the correspondent Array. Can you help me?
Rui Lopes