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: 
I would create a new dict containing two items: the inventory dict and the quantity. I'd add an item to itemsArray like this (untested, so beware of typos):

```
BOOL found = NO;
for (NSDictionary *dict in itemsArray)
{
    if ([[dict objectForKey: @"inventorydict"] isEqual: inventory.itemDict])
    {
        [dict setObject: [NSNumber numberWithInt: 
                          [[dict objectForKey: @"quantity"] intValue] + 1] 
                 forKey: @"quantity"];
        found = YES;
        break;
    }
}

if (!found)
{
    [itemsArray addObject: 
        [NSMutableDictionary dictionaryWithObjectsAndKeys:
            inventory.itemDict, @"inventorydict", 
            [NSNumber numberWithInt: 1], @"quantity",
            nil]];
}

```

So itemsArray contains NSDictionaries with two keys: "inventorydict" and "quantity". "inventorydict" is the dict passed to you containing the item the user bought, and "quantity" is an NSNumber. When you receive a new product item in the basket, you first check if the item is already in the array. If so, you add one to the "quantity" number, otherwise you create a new dictionary with the inventory item and a quantity of 1.
FYI for `setObject:` you do not need to use `stringWithFormat:` if you do not have an object to add to it, you can simply use `setObject:@"1"`.

If you want to increment the **Quantity**, you should be using `setObject:[NSNumber numberWithInt:1]` instead.