【问题标题】:How to Get the Direct Access Index for NSDictionary of NSDictionary如何获取 NSDictionary 的 NSDictionary 的直接访问索引
【发布时间】:2010-02-03 15:04:31
【问题描述】:

代码如下:

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" 的代码是什么?

【问题讨论】:

  • 您的代码中没有数组。
  • 嗨 St3fan,我确实在顶部添加了数组语句
  • 它们仍然不是数组而是字典。什么是 NSLog 格式?
  • Sorry ST3fan,我是说字典,NSLog只是为了回显到控制台,我只需要直接读取 circuit_step '01' step_info 'search-location'

标签: iphone multidimensional-array nsarray nsdictionary


【解决方案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'。

【讨论】:

  • St3fan,据我了解,这 3 行之间有什么区别: 1- NSMutableDictionary* step_info = [NSMutableDictionary dictionary]; 2- NSMutableDictionary* step_info = nil; 3- step_info = [NSMutableDictionary 字典];谢谢
  • 小问题。所有可变集合的首选初始化器是initWithCapacity 的变体。在这种情况下,您可能希望使用“dictionaryWithCapcity;”或``initWithCapacity:`。只需将容量设置为 1 即可启动。使用继承的泛型初始化器有时会导致奇怪的问题。
  • 如果我不放行“step_info = [NSMutableDictionary dictionary];”在 2 个 if 语句段落之间,我在 circuit_step 的 step_info 中为键“01”丢失了值,但它已经保存了。为什么需要这条线?
  • @TechZen 有什么奇怪的问题?我从来没有遇到过不使用那些替代初始化器的奇怪问题。而且,它们不是优选的。只是一种选择。
  • @gpsdev 您需要两个字典,因为没有复制任何内容。父字典只是引用另外两个字典。因此,如果您不创建两个新实例,那么 01 和 02 都将指向同一个实例。
猜你喜欢
  • 1970-01-01
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多