Algorithm: An array of arrays in Cocoa Touch (possibly using an NSCountedSet)

It's a little tedious as far as explaining, so here it is. I essentially populate the tableView on the iPhone with several sections and potentially several rows per section. As far as I understand, it is best to have an array of arrays so that you can simply determine how many partitions one has by sending a message to the top level array count, and then for the lines in the section, doing the same for the internal array (s). My data is in the form of a dictionary. One of the key / value pairs in the dictionary determines where it will be displayed in the View table. An example is as follows:

{
  name: "bob",
  location: 3
}
{ 
  name: "jane",
  location: 50
}
{
  name: "chris",
  location: 3
}

. , bob chris, 3- . jane, 50. Cocoa ? - C, , , , Cocoa.

, , , , .

+3
3

: ( edit: )

NSArray * arrayOfRecords = [NSArray arrayWithObjects:

    [NSDictionary dictionaryWithObjectsAndKeys:
     @"bob", @"name",
      [NSNumber numberWithInt:3], @"location",  nil],

    [NSDictionary dictionaryWithObjectsAndKeys:
     @"jane", @"name",
      [NSNumber numberWithInt:50], @"location",  nil],

    [NSDictionary dictionaryWithObjectsAndKeys:
     @"chris", @"name",
      [NSNumber numberWithInt:3], @"location",  nil],

    nil];

NSMutableDictionary * sections = [NSMutableDictionary dictionary];

for (NSDictionary * record in arrayOfRecords)
{
    id key = [record valueForKey:@"location"];
    NSMutableArray * rows = [sections objectForKey:key];

    if (rows == nil)
    {
        [sections setObject:[NSMutableArray arrayWithObject:record] forKey:key];
    }
    else
    {
        [rows addObject:record];
    }
}

NSArray * sortedKeys = [[sections allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSArray * sortedSections = [sections objectsForKeys:sortedKeys notFoundMarker:@""];

NSLog(@"%@", sortedSections);
+5

NSDictionary - -.

+3

Cocoa , , Bindings. , , , . ( .)

Cocoa , ( Cocoa, iPhone), . .

( "" Cocoa "Model-View-Controller", Cocoa , .)

0

All Articles