Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios - get values from NSDictionary

I have JSON on my server, which is parsed into iOS app to NSDictionary. NSDictionary looks like this:

(
        {
        text = Aaa;
        title = 1;
    },
        {
        text = Bbb;
        title = 2;
    }
)

My question is - how to get just text from first dimension, so it should be "Aaa". I've tried to use this:

[[[json allValues]objectAtIndex:0]objectAtIndex:0];

But it didn't work, it ends with error

Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFArray allValues]:
unrecognized selector sent to instance 0x714a050'

So can you help me please, how to get just one value from specified index? Thanks!

like image 896
stepik21 Avatar asked Dec 20 '22 00:12

stepik21


2 Answers

That error message is simply telling you that NSDictionary (which is the first object of that array, along with the second) doesn't respond to objectAtIndex.

This will be a bit cody, but it explains it better:

NSArray *jsonArray = [json allValues];
NSDictionary *firstObjectDict = [jsonArray objectAtIndex:0];
NSString *myValue = [firstObjectDict valueForKey:@"text"];
like image 88
LJ Wilson Avatar answered Dec 24 '22 10:12

LJ Wilson


Your JSON object is an array, containing two dictionaries. That's how to get the values:

NSDictionary* dict1= json[0];
NSString* text= dict1[@"text"];
NSString* title= dict1[@"title"];
like image 26
Ramy Al Zuhouri Avatar answered Dec 24 '22 10:12

Ramy Al Zuhouri