Question: I'm trying to grasp the best approach to saving (serializing) and restoring (deserializing) a DOM selection or range. There is a post here on stringifying a range object, but it frankly doesn't really work.

The situation is an interactive text book using the Webkit view of Adobe Air. I have static (built-in) html content that the user can highlight and bookmark (annotate). This mechanism all works, but I need to be able to store and restore these annotations. I would rather not store a modified version of the DOM, but rather use the static version then reapply the users annotations that I am storing in a SQLite DB along with other needed metadata. DOM manipulation is pretty new for me, and so far my attempts to serialize a DOM::range have failed. What I realized though is that I really seem to only need the start and end containers and the start and end offsets. Then I can recreate the range with document.createRange().

What I could use guidance on is the best approach to serializing the start and end containers. My first thought was xpath, but so far my attempts have come up short. Looking at the Mozilla docs for DOM::Range seems pretty straight forward, but creating a reliable xpath to restore a range isn't quite clicking for me.

Answer: 
Store your dictionaries in an `NSCountedSet`. You can then get the quantity via `-countForObject:`. Use an array only for presentation purposes, so you can sort the values in a sane way. Rebuild this array whenever the set changes, something like so:

```
- (void)itemsDidChange
{
    NSCountedSet *itemSet = [self itemSet];
    NSMutableArray *sortedItems = [[NSMutableArray alloc] init];
    for (NSDictionary *item in itemSet) {
        NSUInteger count = [itemSet countForObject:item];
        NSNumber *countNum = [[NSNumber alloc] initWithUnsignedInteger:count];
        NSMutableDictionary *arrayItem = [item mutableCopy];
        [arrayItem setObject:countNum forKey:KEY_QUANTITY];
        [countNum release];
        [sortedItems addObject:arrayItem];
        [arrayItem release];
    }
    [sortedItems sortUsingComparator:/* your comparator here */];
    [self setRowItems:sortedItems];
    [[self tableView] reloadData];
}

```

Even simpler, use the object directly in your array without changing it at all. When you present the quantity to the user in the UI, just query the `itemSet` for the count, and use that. The array is then used solely to impose an order on the set's items.
based on Rudy's post made it working this way:

```
-(void)addToCart:(id)sender 
    {

    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    BOOL found = NO;

    NSString *itemName = // get your item name here

    [theDict setObject:itemName forKey:@"name"];

    if ([appDelegate.theCartArray isEqual:nil]) 
    {    
        [appDelegate.theCartArray addObject:theDict];
    }

    else //implementing Rudy's idea
    {

    for (theDict in appDelegate.theCartArray) 
    {
        if ([[theDict objectForKey:@"name"] isEqual:itemName]) 
        {
            [theDict setObject: [NSNumber numberWithInt:
                                [[theDict objectForKey: @"quantity"] intValue] + 1] 
                                forKey: @"quantity"];

            found = YES;

            break;
        }
    }

        if (!found) 
        {
            [appDelegate.theCartArray addObject: 
                             [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                                                  itemName, @"name", 
                                                  [NSNumber numberWithInt: 1], @"quantity",
                                                                                     nil]];
        }
    }
}

```