ios - NSMutableArray 中的 NSDictionary (iOS)

标签 ios objective-c nsmutablearray nsdictionary

<分区>

我需要以下结果:

(

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

关于ios - NSMutableArray 中的 NSDictionary (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15574772/

相关文章:

ios - Xcode Singleton Undeclared Identifier 放大器

iphone - TTURL请求和发送POST数据

objective-c - 如何检索文本字段文本并将其作为整数 32 存储在核心数据中?

iphone - 无法在字典中添加 Imageview 引用来查看

objective-c - 为什么在调用类方法之前通过+(Class)class发送一个类?

ios - 在肖像中,它工作正常,但在风景中,它出现在半屏上,我正在使用 wAny hAny 尺寸类别

iphone - NSMutable 数组 - 分配和保留对象

ios - 如何在Objective-C中创建多个锯齿状数组

nsmutablearray - Sprite Kit iOS7 - SKNode UserData 属性不存储值

ios - UIViewController 中的 subview 高度