6

结果,我需要以下内容:

(

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

任何的想法?谢谢!

4

5 回答 5

16

你的错误在这里:

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"}}];
于 2013-03-22T16:05:04.437 回答
4

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"];
于 2013-03-22T16:09:32.247 回答
2

从文档

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

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

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

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

于 2013-03-22T16:15:58.040 回答
2

数组不使用setValue:forKey:(nor 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>

`

于 2013-03-22T16:19:37.940 回答
2

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

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];
于 2013-03-22T16:20:24.340 回答