NSDictionary inside NSMutableArray (iOS)

I need the following:

(

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

);

For this, I have this code, but it does not work:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"another_value", @"another_key", nil];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array setValue:dictionary forKey:@"some_key"];

Any idea? Thank!

+5
source share
5 answers

Your mistake:

NSMutableArray *array = [[NSMutableArray alloc] init];
[array setValue:dictionary forKey:@"some_key"];

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

You set this to an array.

Try the following:

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

In new literals:

NSDictionary *d=@{@"another_key":@"another_value"};
NSDictionary *c=@{@"some_key":d};
NSArray *array=@[c];

Or nested creation:

NSArray *array=@[@{@"some_key":@{@"another_key":@"another_value"}}];
+15
source

An NSMutableArraycan usually be built by simply adding objects to the end. methodaddObject:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"another_value", @"another_key", nil];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:dictionary];

On the other hand, if you want to access the dictionary using the key (@ "some_key"), you will also need an external container for the dictionary:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"another_value", @"another_key", nil];
NSMutableDictionary *outerDict = [NSMutableDictionary dictionary];
[outerDict setObject:dictionary forKey:@"some_key"];
+4
source

setValue: forKey: - , .

NSKeyValueCoding. , NSArray - , . , :

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

, NSDictionary a NSArray.

+2

setValue:forKey: ( setObject:forKey:), , .

setValue:forKey: - KVC (Key Value Coding).

( )

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

All Articles