Ios - get values ​​from NSDictionary

I have JSON on my server that is parsed in an iOS application for NSDictionary. NSDictionary is as follows:

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

My question is: how to get only text from the first dimension, so it should be "Aaa". I tried using this:

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

But it didn’t work, it ends in an error

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

So can you help me how to get only one value from a given index? Thanks!

+4
source share
3 answers

Your JSON object is an array containing two dictionaries. How to get the values:

 NSDictionary* dict1= json[0]; NSString* text= dict1[@"text"]; NSString* title= dict1[@"title"]; 
+6
source

This error message simply tells you that NSDictionary (which is the first object of this array along with the second) does not respond to objectAtIndex .

This will be a bit of codi, but this explains it better:

 NSArray *jsonArray = [json allValues]; NSDictionary *firstObjectDict = [jsonArray objectAtIndex:0]; NSString *myValue = [firstObjectDict valueForKey:@"text"]; 
+6
source

Try the following:

 NSString *txt = [[json objectAtIndex:0] objectForKey:@"text"]; 

UPDATE: Bug fixed. Thanks yunas.

0
source

Source: https://habr.com/ru/post/1501161/


All Articles