【问题标题】:NSDictionary inside NSMutableArray (iOS) [closed]NSMutableArray (iOS) 中的 NSDictionary [关闭]
【发布时间】:2013-03-22 16:01:43
【问题描述】:

因此,我需要以下内容:

(

    "some_key" = {
        "another_key" = "another_value";
    };

);

为了做到这一点,我有这段代码,但它不起作用:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"another_value", @"another_key", nil];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array setValue:dictionary forKey:@"some_key"];

有什么想法吗?谢谢!

【问题讨论】:

  • 当我NSLog数组时,它返回()
  • 您提供的示例混合了数组和字典语法。不清楚结果应该是字典还是数组。
  • @NikolaiRuhe 实际上,setValue:forKey: 作为NSArrays 的KVC 访问器是完全有效的。由于数组是空的,所以它什么也不做。
  • @Monolo 我指的是上面的日志输出。另外,我认为在字典上使用 setValue:forKey: 作为普通访问器是非常糟糕的风格。

标签: ios objective-c nsmutablearray nsdictionary


【解决方案1】:

你的错误在这里:

NSMutableArray *array = [[NSMutableArray alloc] init];
[array setValue:dictionary forKey:@"some_key"];

------------^^^^^

您正在将其设置为数组。

试试这个:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"another_value", @"another_key", nil];
NSDictionary *outDict=[[NSDictionary alloc]initWithObjectsAndKeys:dictionary,@"some_key", nil];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:outDict, nil];

在新的文字中:

NSDictionary *d=@{@"another_key":@"another_value"};
NSDictionary *c=@{@"some_key":d};
NSArray *array=@[c];

或嵌套创建:

NSArray *array=@[@{@"some_key":@{@"another_key":@"another_value"}}];

【讨论】:

  • 我最喜欢嵌套的创作。
【解决方案2】:

NSMutableArray 通常可以通过在末尾添加对象来构建。方法是addObject:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"another_value", @"another_key", nil];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:dictionary];

另一方面,如果您想通过键(@“some_key”)来寻址字典,那么您也需要外部容器是字典:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"another_value", @"another_key", nil];
NSMutableDictionary *outerDict = [NSMutableDictionary dictionary];
[outerDict setObject:dictionary forKey:@"some_key"];

【讨论】:

    【解决方案3】:

    来自文档

    setValue:forKey: - 设置由 a 指定的接收者的属性 给定值的给定键。

    那是NSKeyValueCoding 协议。您使用错误的方法将对象添加到数组中,NSArray 是有序集合,而不是字典。做你需要的最简单的方法是:

    NSDictionary* dic = @{@"some_key": @{@"another_key": @"another_value"}};
    

    如您所见,输出是NSDictionary 而不是NSArray

    【讨论】:

      【解决方案4】:

      数组不使用setValue:forKey:(也不使用setObject:forKey:)并且数组不像字典那样具有关联性。

      setValue:forKey: 是 KVC(键值编码)。

      你想要一个字典数组(下面是伪 plist 形式)

      <array>
        <dict>
          <key>someKey</key>
          <dict>
            <key>someOtherKey</key>
            <string>someValue</string>
          </dict>
        </dict>
        <dict>
          <key>someKey</key>
          <dict>
            <key>someOtherKey</key>
            <string>someValue</string>
          </dict>
        </dict>
        <dict>
          <key>someKey</key>
          <dict>
            <key>someOtherKey</key>
            <string>someValue</string>
          </dict>
        </dict>
        <dict>
          <key>someKey</key>
          <dict>
            <key>someOtherKey</key>
            <string>someValue</string>
          </dict>
        </dict>
      </array>
      

      `

      【讨论】:

        【解决方案5】:

        我认为您正在寻找这种结构

        NSDictionary *anotherKeyValueDictionary = 
         [[NSDictionary alloc] initWithObjectsAndKeys:@"another_value", 
                                                      @"another_key", nil];
        NSDictionary *someKeyValueDictionary = 
         [[NSDictionary alloc] initWithObjectsAndKeys:@"anotherKeyValueDictionary", 
                                                      @"some_key", nil];
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [array addObject:someKeyValueDictionary];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多