views:

447

answers:

1

Here's the code:

NSMutableDictionary *circuit_step = [NSMutableDictionary dictionary];    
NSMutableDictionary *step_info    = [NSMutableDictionary dictionary];

 [step_info setObject: @"search"   forKey: @"search-type"];     
 [step_info setObject: @"small"   forKey: @"search-format"];     
 [step_info setObject: @"winter"   forKey: @"search-season"];    
 [step_info setObject: @"tree"   forKey: @"search-location"];

 **[circuit_step setObject: circuit_step forKey: @"01"];**

 [step_info setObject: @"search"   forKey: @"search-type"]; 
 [step_info setObject: @"micro"   forKey: @"search-format"];     
 [step_info setObject: @"summer"   forKey: @"search-season"];    
 [step_info setObject: @"by the lake"          forKey: @"search-location"];

 **[circuit_step setObject: circuit_step forKey: @"02"];**

What is the code to do a direct access on dictionary circuit_step key "01" and dictionary step_info key "search-location" in a format suitable for NSLog?

+1  A: 

What about

NSLog(@"Value is %@",
    [[circuit_step objectForKey: @"01"] objectForKey: @"search-location"])

Also, your code is all wrong. Here is a fixed version:

NSMutableDictionary *circuit_step = [NSMutableDictionary dictionary];
if (circuit_step != nil)
{
    NSMutableDictionary* step_info = nil;

    step_info = [NSMutableDictionary dictionary];
    if (step_info != nil) {
        [step_info setObject: @"search" forKey: @"search-type"];
        [step_info setObject: @"small" forKey: @"search-format"];
        [step_info setObject: @"winter" forKey: @"search-season"];
        [step_info setObject: @"tree" forKey: @"search-location"];
        [circuit_step setObject: step_info forKey: @"01"];
    }

    step_info = [NSMutableDictionary dictionary];
    if (step_info != nil) {     
        [step_info setObject: @"search" forKey: @"search-type"];
        [step_info setObject: @"micro" forKey: @"search-format"];
        [step_info setObject: @"summer" forKey: @"search-season"];
        [step_info setObject: @"by the lake" forKey: @"search-location"];
        [circuit_step setObject: step_info forKey: @"02"];
    }
}

You were not setting the right object in circuit_step and you were also reusing a dictionary so you would end up with two entries pointing to the same dictionary with the values of '02'.

St3fan
Thank's St3fan, codes is working great !GPSDEV
St3fan, for my understanding, what is the difference between those 3 lines:1- NSMutableDictionary* step_info = [NSMutableDictionary dictionary];2- NSMutableDictionary* step_info = nil;3- step_info = [NSMutableDictionary dictionary];Thank's
Minor quibble. The preferred initializers for all mutable collects are the variants of `initWithCapacity`. In this case, you would want to use either 'dictionaryWithCapcity;' or ``initWithCapacity:`. Simple set the capacity to 1 to start. Using an inherited generic initializer can sometimes lead to odd problems.
TechZen
If I don't put the line "step_info = [NSMutableDictionary dictionary];" between the 2 if statement paragraph, I loose values in step_info of circuit_step for key "01", but it is already saved. Why that line is required ?
@TechZen what kind of odd problems? I have *never* encountered any odd problems by not using those alternative initializers. Also, they are not preferred. Simply an alternative.
St3fan
@gpsdev you need two dictionaries because nothing is copied. The parent dictionary simply *references* the two other dictionaries. So if you do not create two new instances then both 01 and 02 will point to the same single instance.
St3fan
Ok I got the idea, in PHP that would have worked, but now I need to get used of the memory pointing mechanism with objective-c.Thank's for your help again St3fan !
@St3fan -- usually failure to respond to selectors and memory issues. On the other hand, it might be a fossil issue. Class specific initializers where something drilled into me when I was learning NextStep 10 years ago at Apple. Perhaps in Objective-C 2.0 it's no longer an issue
TechZen
@TechZen - I'm sure it has been fixed. The documentation specifically states that `dictionary` may be used to create mutable dictionaries.
St3fan