开发者

How do I copy individual Nested NSDictionary's from a NSDictionary to a temporary NSMutableDictionary

Ok so I want to create a temporary NSDictionary fr开发者_JAVA技巧om a NSDictionary of nested dictionaries, but I want to deep copy individual items(dictionaries) from the top level dictionary.

The end result is to have a filtered dictionary that i can process and discard without effecting the main dictionary.

That sounds really confusing, so how about a little code to show you what I mean, heres the function i'm working on, this is a rough coding layout, but basically complete in its path of process.

I've looked at reference books and various samples online with no joy.

Cheers, Darren

- (void)setPricingData
{
    // get selected lens option
    NSDictionary *aOption = [self.lensOptionsDict objectAtIndex:self._lensOptionsIndex];
    if ( aOption == nil )
        return;

    // get selected lens type
    NSDictionary *aType = [self.lensTypesDict objectAtIndex:self._lensTypesIndex];
    if ( aType == nil )
        return;

    // get lens option id and variation_id
    NSString *option_id = [aOption valueForKey:@"id"];
    NSString *option_variation_id = [aOption valueForKey:@"variation_id"];

    // create temp dictionary for type pricing selection
    int count = [self.lensTypesDict count];
    NSMutableDictionary *aPrices = [[NSMutableDictionary alloc] initWithCapacity:count];

    // cycle prices for option id and variation_id matches
    for ( NSDictionary *item in self.pricesDict )
    {
        NSString *variation_id = [item valueForKey:@"variation_id"];
        NSString *value_id = [item valueForKey:@"value_id"];

        // add matches to temp dictionary
        if ( [option_variation_id isEqualToString: variation_id] )
        {
            if ( [option_id isEqualToString: value_id] )
                [aPrices addObject: item];
        }
    }

    // get price from temp dictionary for selected lens type index
    NSDictionary *price = [aPrices objectAtIndex:self._lensTypesIndex];
    if ( price != nil )
    {
        // assign values to outlet
        self.priceAndStockId = [price valueForKey:@"price"];
        self.priceSelected = [price valueForKey:@"price"];
    }

    // release temp dictionary
    [aPrices release];
}


It looks like you're mixing up dictionaries with arrays.

Arrays respond to objectAtIndex whereas dictionaries respond to objectForKeys. Remember that an array is a set of cells that you can index into, starting from 0 all the way up to [array count] - 1.

A dictionary is similar to an array, except that a hash function is used as the indexing method. This means that you need a key to get, or set, and object.

Setting an object in an NSMutableDictionary

NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
[myDictionary setObject:anObject forKey:aKey];

Or, you can have an array of keys and corresponding array of objects, and do:

NSDictionary *completeDictionary;
completeDictionary = [NSDictionary  dictionaryWithObjects:objectArray 
                      forkeys:keyArray count:[keyArray count]];

In either case, you must have keys for objects. This is in contrast to a regular array in which you can simply do

[myArray addObject:myObject];

To get objects from a dictionary, do

myObject = [myDictionary objectForKey:key];

To get objects from an array, do

myObject = [myArray objectAtIndex:anIntegerIndex];

Finally, your original question pertained to deep copying. To have your dictionary keep an object that won't change, ie, a deep copy, you can do the following:

Assuming I want to store a dictionary within a dictionary, and I have an associated key for the top-level dictionary, I can do the following:

I have an NSMutableDictionary, called topLevelDictionary I have an NSDictionary, called dictionaryTwo I have an NSString, which is my key, called myKey.

To make a deep copy of dictionaryTwo, I can do

// assuming topLevelDictionary is previously defined
[topLevelDictionary setObject:[[dictionaryTwo copy] autorelease] forKey:myKey];

In this manner topLevelDictionary will contain a copy of dictionaryTwo whereby if dictionaryTwo changes, the object in topLevelDictionary will not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜