-1

I have an NSArray that looks like this

NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"main: %@", [dataDict valueForKey:@"main"]);
main: [{"city": "dallas", "description": "this is my test description"}]

And I am trying to turn it into a NSDictionary so I get get element like [mainDict objectForKey@"city"]

So I tried this

NSLog(@"%@", [[mainArray objectAtIndex:0] objectForKey:@"city"]);

Thinking it would get the first element in mainArray and then the object that I want but I keep getting this error

[__NSCFString objectAtIndex:]: unrecognized selector sent to instance

How can I make the NSAarry into a NSDictionary?

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
spen123
  • 3,464
  • 11
  • 39
  • 52
  • @DevangGoswami I got the same error – spen123 Dec 08 '15 at 08:01
  • mainArray is string not a array so please first convert it into array and then do your code. It will work perfect – Dharmbir Singh Dec 08 '15 at 08:01
  • nsdictionary *abc=@{@"city": @"dallas", @"description": @"this is my test description"} – Devang Goswami Dec 08 '15 at 08:04
  • @DevangGoswami I am not defining `mainArray` like that, that is what I see when I NSLog it – spen123 Dec 08 '15 at 08:06
  • Possible duplicate of [Convert NSArray to NSDictionary](http://stackoverflow.com/questions/1414852/convert-nsarray-to-nsdictionary) – Jens Dec 08 '15 at 08:07
  • @Jens my question is different than the one you linked to as I am getting my array from a dictionary to start with – spen123 Dec 08 '15 at 08:13
  • The Content of your question is completely changed. And after the Change it is very unclear. You get a NSDictionary and want to change it into a NSDictionary ?? – Jens Dec 08 '15 at 08:15
  • lol just make a for loop to fetch data from it .. for (NSDictionary *d in mainArray){ nslog (@"%@",[d objectForKey :@"key"]); } – Devang Goswami Dec 08 '15 at 08:16
  • @Jens I just added a few lines to show I was not declaring the mainArray, I am getting it from another NSDictionary, which was my mistake in not explaining correctly first, but not I think this should be reopened – spen123 Dec 08 '15 at 08:16
  • @DevangGoswami now I get this error `[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance` – spen123 Dec 08 '15 at 08:18
  • print d put a break point there if that dont work then change loop : for (NSDictionary *d in [[dataDict valueForKey:@"main"] objectAtIndex:0]) – Devang Goswami Dec 08 '15 at 08:21
  • @spenf10 Please have a look on my answer. – Dharmbir Singh Dec 08 '15 at 08:58

3 Answers3

0

Create a for loop for list when you get response in [{},{}] format that means its a list of elements . Or you can ask your API developer to correct the error .

Any ways simple solution here is :

for (NSDictionary *d in [dataDict valueForKey:@"main"]){ 
NSlog (@"%@",d); 
NSlog (@"%@",[d objectForKey :@"city"]); 
} 
Devang Goswami
  • 284
  • 3
  • 23
0

Your error is saying mainarray is not a NSArray, it is NSString. So please check like this

    NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSLog(@"main: %@", [dataDict valueForKey:@"main"]);


    if ([[dataDict valueForKey:@"main"] isKindOfClass:[NSArray class]]) {

        NSArray *mainArray = [dataDict valueForKey:@"main"];

        NSLog(@"City == %@",[[mainArray objectAtIndex:0] objectForKey:@"city"]);
    }else{
        NSLog(@"mainarray is kind of -> %@", NSClassFromString([dataDict valueForKey:@"main"]));
    }
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
-3
NSArray *mainArray = @[@{@"city":@"dallas", @"description":@"this is my test description"}];
 NSLog(@"%@", [[mainArray objectAtIndex:0] objectForKey:@"city"]);

no problem ....

Flexo
  • 87,323
  • 22
  • 191
  • 272