Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDictionary inside NSMutableArray (iOS) [closed]

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!

like image 799
IOS_DEV Avatar asked Mar 22 '13 16:03

IOS_DEV


People also ask

What is the use of nsmutablearray?

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.

What is the NSMutableDictionary class?

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.

What is an NSDictionary object?

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.

What is NSDictionary in Xcode?

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.


Video Answer


1 Answers

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"}}];
like image 122
Anoop Vaidya Avatar answered Oct 05 '22 00:10

Anoop Vaidya