Question

I have created a plist file that looks like this:

enter image description here
From this is am able to extract all plist info into an NSArray:

-(NSArray *)Topics
{
    if(!_Topics)
    {
        _Topics = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TopicData" ofType:@"plist"]];
    }
    return _Topics;
}

And use this array to load a tableview of TopicTitle's:

cell.textLabel.text = [[self.Topics objectAtIndex:indexPath.row] valueForKey:@"TopicTitle"];

When a row in the table is selected, I am passing the dictionary named 'Questions' to the next ViewController like this:

NSDictionary *questions = [[self.Topics objectAtIndex:indexPath.row] valueForKey:@"Questions"];
[self.detailViewController setQuestions:(questions)];

From here I want to loop through each 'Question' dictionary and load the 'QuestionText' and 'AnswerOne / Two...' strings into an array of objects by doing something like this:

TopicQuestions = [NSMutableArray array];
for(NSDictionary *ques in self.Questions)
{
    Question* q = [[Question alloc] init];
    q.Question = (NSString*)[ques objectForKey:@"QuestionText"];
    q.AnswerOne = (NSString*)[ques objectForKey:@"QuestionText"];
    q.AnswerTwo = (NSString*)[ques objectForKey:@"QuestionText"];
    q.AnswerThree = (NSString*)[ques objectForKey:@"QuestionText"];
    q.AnswerFour = (NSString*)[ques objectForKey:@"QuestionText"];
    [TopicQuestions addObject:q];
}

But the 'Questions' dictionary does not seem to have this data available, it knows there are 4 child objects, but does not have all the key - pair values of these objects:

enter image description here

So my question is how should I pass the 'Questions' dictionary to the next ViewController so that I will still be able to access the 'QuestionText' and 'AnswerOne / Two...' nodes?

Or is there a better way of reading the strings without looping through each 'Question' dictionary?

Was it helpful?

Solution

I don't think you are getting the question dictionary properly. In your screenshot the ques object is an NSString object, not an NSDictionary. So getting objectForKey on the NSString object ques is not going to work (and really should crash your app).

Try this for loop:

for(NSString *ques in self.Questions)
{
    NSDictionary *dict = [self.questions objectForKey:ques];
    Question* q = [[Question alloc] init];
    q.Question = (NSString*)[dict objectForKey:@"QuestionText"];
    q.AnswerOne = (NSString*)[dict objectForKey:@"QuestionText"];
    q.AnswerTwo = (NSString*)[dict objectForKey:@"QuestionText"];
    q.AnswerThree = (NSString*)[dict objectForKey:@"QuestionText"];
    q.AnswerFour = (NSString*)[dict objectForKey:@"QuestionText"];
    [TopicQuestions addObject:q];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top