PlugInBoy
PlugInBoy

Reputation: 989

Convert NSDictionary to NSMutableArray

I have this code:

NSDictionary *jsonGuardado = [self dictionaryFromJson:JSON];
NSLog(@"HOLA %@",jsonGuardado);
NSMutableArray *arrayCodigos = [jsonGuardado objectForKey:@"code"];
codigos = arrayCodigos;

(Codigos is an NSMutableArray)

The NSLOG return this:

HOLA (
        {
        code = 22051310;
    },
        {
        code = 22051311;
    },
        {
        code = 22051312;
    },
        {
        code = 22051313;
    }
)

But right after this, a message error says:

-[JKArray objectForKey:]: unrecognized selector sent to instance

I search this error in Google and here and all the questions I found not help me.

I'm using the JSONKit for make the dictionaryFromJson method.

Upvotes: 3

Views: 15227

Answers (3)

Augustine P A
Augustine P A

Reputation: 5068

You try this,

NSArray *jsonGuardado = [self dictionaryFromJson:JSON];
NSLog(@"HOLA %@",jsonGuardado);
NSMutableArray *arrayCodigos = [NSMutableArray arrayWithArray:[jsonGuardado valueForKey:@"code"]];
NSLog(@"arrayCodigos %@", arrayCodigos);

It will print only values.

Upvotes: 6

Rahul Patel
Rahul Patel

Reputation: 5886

Your log says that it's already NSArray. You should try below

NSMutableArray *jsonGuardado = [NSMutableArray array];
jsonGuardado = (id)[self dictionaryFromJson:JSON];
NSLog(@"HOLA %@",jsonGuardado);
NSMutableArray *arrayCodigos = [[jsonGuardado objectAtIndex:0] valueForKey:@"code"];
codigos = [arrayCodigos copy];
NSLog(@"Array %@",codigos);

Upvotes: 1

thatzprem
thatzprem

Reputation: 4767

The parser returns an array and not a dictionary in your case.

[self dictionaryFromJson:JSON] returns a Json array but you are capturing it in NSDictionary

Upvotes: 2

Related Questions