NSMutableArray(iOS)中的NSDictionary

NSMutableArray(iOS)中的NSDictionary,ios,objective-c,nsmutablearray,nsdictionary,Ios,Objective C,Nsmutablearray,Nsdictionary,因此,我需要以下信息: ( "some_key" = { "another_key" = "another_value"; }; ); 为此,我有以下代码,但它不起作用: NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"another_value", @"another_key", nil]; NSMutableArray *array = [[NSMutab

因此,我需要以下信息:

(

    "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"];
有什么想法吗?谢谢

您的错误在这里:

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

NSMutableArray
通常只需在末尾添加对象即可构建。方法是
addObject:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"another_value", @"another_key", nil];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:dictionary];
另一方面,如果希望通过键(“some_键”)对字典进行寻址,那么外部容器也需要是字典:

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

setValue:forKey:-设置由 给定值的给定键

这是
NSKeyValueCoding
协议。您使用了错误的方法将对象添加到数组,
NSArray
是有序集合,而不是字典。完成所需任务的最简单方法是:

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

如您所见,输出是一个
NSDictionary
而不是
NSArray
数组不使用
setValue:forKey:
(也不使用
setObject:forKey:
),数组与字典不相关

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

您需要一个字典的数组(以下为伪plist形式)


某物
其他人
一些价值
某物
其他人
一些价值
某物
其他人
一些价值
某物
其他人
一些价值

`

我想您正在寻找这种结构

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

当I
NSLog
数组时,它返回
()
。您提供的示例混合了数组和字典语法。不清楚结果是字典还是数组。@NikolaiRuhe实际上,
setValue:forKey:
作为
NSArray
s的KVC访问器是完全有效的。因为数组是空的,所以它什么也不做,不过我指的是上面的日志输出。此外,我认为使用<代码> StValu:FordK::/Cord>作为一个普通的访问器是非常糟糕的风格。我喜欢嵌套的创建是最好的。
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];