I need the following as a result:
(
"some_key" = {
"another_key" = "another_value";
};
);
In order to do so, I have this code, but it doesn't work:
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"another_value", @"another_key", nil];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array setValue:dictionary forKey:@"some_key"];
Any Idea? Thanks!
The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray. NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray.
The NSMutableDictionary class declares the programmatic interface to objects that manage mutable associations of keys and values. It adds modification operations to the basic operations it inherits from NSDictionary. NSMutableDictionary is “toll-free bridged” with its Core Foundation counterpart, CFMutableDictionary.
An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics. The NSDictionary class declares the programmatic interface to objects that manage immutable associations of keys and values.
NSDictionary help us in creating static dictionary. Modern Objective C Dictionary notations. There is modern Objective C syntax that makes it easier for us to create and retrieve objects. Xcode actually changes to required format in build time.
Your error is here:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array setValue:dictionary forKey:@"some_key"];
------------^^^^^
You are setting this into array.
Try this one:
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"}}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With