2013-03-22 67 views
4

我需要以下的结果:的NSDictionary内的NSMutableArray(IOS)

(

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

任何理念?谢谢!

+0

的当我' NSLog'数组,它返回'()'。 – 2013-03-22 16:06:51

+1

您提供了混合数组和字典语法的示例。目前还不清楚结果应该是字典还是数组。 – 2013-03-22 16:13:28

+0

@NikolaiRuhe实际上,'setValue:forKey:'作为NSArray的KVC访问器是完全有效的。由于该数组是空的,它什么都不做。 – Monolo 2013-03-22 16:15:05

回答

14

你的错误是在这里:

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 *[email protected]{@"another_key":@"another_value"}; 
NSDictionary *[email protected]{@"some_key":d}; 
NSArray *[email protected][c]; 

或嵌套创作:

NSArray *[email protected][@{@"some_key":@{@"another_key":@"another_value"}}]; 
+0

我喜欢嵌套创作最好。 – 2013-03-22 16:20:36

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

从文档

的setValue:forKey: - 设置由 给钥匙给定值指定的接收器的性能。

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

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

正如你可以看到输出是NSDictionary不是NSArray

2

数组的不使用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> 

`

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]; 
相关问题