NSMutableArray(iOS)中的NSDictionary

IOS*_*DEV 6 objective-c nsdictionary nsmutablearray ios

结果我需要以下内容:

(

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

);
Run Code Online (Sandbox Code Playgroud)

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

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"another_value", @"another_key", nil];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array setValue:dictionary forKey:@"some_key"];
Run Code Online (Sandbox Code Playgroud)

任何的想法?谢谢!

Ano*_*dya 16

你的错误在这里:

NSMutableArray *array = [[NSMutableArray alloc] init];
[array setValue:dictionary forKey:@"some_key"];
Run Code Online (Sandbox Code Playgroud)

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

您将此设置为数组.

试试这个:

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];
Run Code Online (Sandbox Code Playgroud)

在新的文字中:

NSDictionary *d=@{@"another_key":@"another_value"};
NSDictionary *c=@{@"some_key":d};
NSArray *array=@[c];
Run Code Online (Sandbox Code Playgroud)

或者嵌套创建:

NSArray *array=@[@{@"some_key":@{@"another_key":@"another_value"}}];
Run Code Online (Sandbox Code Playgroud)