Sagar
Sagar

Reputation: 1304

-[__NSDictionaryM objectAtIndex:]: unrecognized selector sent to instance

I searched for all possible combinations and answers on SO, but didn't helped me a lot. All are related to UITableView only.

What I am doing is I am fetching my Quiz from server. Quiz contain Sections and each section contain Questions. So flow is like this. Quiz->Section->Question

I am getting JSON like first total number of Quiz, then total number of sections in particular Quiz and total number of Questions in particular section.

I am storing Sections in sections array and parsing questions according to section array count. but My app is crashing at point, where I am getting object of Question from section array and storing it in NSMutableDictionary with key to get connected with DTO.

Here is my code..

NSArray *sectionsA = [quizD objectForKey:@"sections"];
NSMutableArray *sectionDTOA = [[NSMutableArray alloc]init];
NSMutableArray* questionDTOA=[[NSMutableArray alloc] init];

for (int i =0; i<[sectionsA count]; i++)
{
NSMutableDictionary* sectionD = [sectionsA objectAtIndex:i];
SectionDTO* sectionDTO = [[SectionDTO alloc]init];
[sectionDTO setSection_id:[sectionD objectForKey:@"sectionID"]];
[sectionDTO setSection_name:[sectionD objectForKey:@"sectionDTitle"]];
[sectionDTO setSection_description:[sectionD objectForKey:@"sectionDDesc"]];
[sectionDTO setSectionCutoff:[sectionD objectForKey:@"sectionCutoff"]];

// code for setting Questions goes below and after that for loop closed..

my app is crashing at line

NSMutableDictionary* sectionD = [sectionsA objectAtIndex:i];

Saying that

-[__NSDictionaryM objectAtIndex:]: unrecognized selector sent to instance 

Whats is wrong in my code, I tried by getting array and then key value pair, but didn't helped. Please guide me.

Thanks in advance.

Upvotes: 0

Views: 4091

Answers (1)

simalone
simalone

Reputation: 2768

NSArray *sectionsA = [quizD objectForKey:@"sections"];

the return value should be a NSDictionary, not NSArray.

Following demo code maybe help you:

NSArray *sectionsA = nil;  
id sectionsObj = [quizD objectForKey:@"sections"];
if([sectionsObj isKindOfClass:[Dictionary class]]){
     sectionsA = sectionsObj[@"keyForArray"];  //@"keyForArray" is the key for array
}
else if([sectionsObj isKindOfClass:[NSArray class]]){
    sectionsA = sectionsObj;
}

Upvotes: 2

Related Questions