Как получить индекс прямого доступа для NSDictionary из NSDictionary - PullRequest
0 голосов
/ 03 февраля 2010

Вот код:

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

Какой код используется для прямого доступа к dictionary circuit_step key "01" и dictionary step_info key "search-location" в формате, подходящем для NSLog?

1 Ответ

1 голос
/ 03 февраля 2010

А как же

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'.

...