如何获取NSDictionary的NSDictionary的直接访问索引

时间:2010-02-03 15:04:31

标签: iphone multidimensional-array nsarray nsdictionary

以下是代码:

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"];**

以适合NSLog的格式对dictionary circuit_step key "01"dictionary step_info key "search-location"进行直接访问的代码是什么?

1 个答案:

答案 0 :(得分:1)

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

另外,你的代码都错了。这是一个固定版本:

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"];
    }
}

您没有在circuit_step中设置正确的对象,而且您还在重复使用字典,因此最终会有两个条目指向同一个字典,其值为“02”。