我创建了一个如下所示的plist文件:
从这里可以将所有plist信息提取到NSArray中:
-(NSArray *)Topics
{
if(!_Topics)
{
_Topics = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TopicData" ofType:@"plist"]];
}
return _Topics;
}
并使用此数组加载TopicTitle的表视图:
cell.textLabel.text = [[self.Topics objectAtIndex:indexPath.row] valueForKey:@"TopicTitle"];
当选择表中的一行时,我将名为“Questions”的字典传递给下一个ViewController,如下所示:
NSDictionary *questions = [[self.Topics objectAtIndex:indexPath.row] valueForKey:@"Questions"];
[self.detailViewController setQuestions:(questions)];
从这里开始,我想遍历每个'Question'字典并通过这样的方式将'QuestionText'和'AnswerOne / Two ......'字符串加载到一个对象数组中:
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];
}
但是'问题'字典似乎没有这个数据可用,它知道有4个子对象,但没有这些对象的所有键对值:
所以我的问题是如何将'Questions'字典传递给下一个ViewController,这样我仍然可以访问'QuestionText'和'AnswerOne / Two ......'节点?
或者有没有更好的方法来阅读字符串而不循环遍历每个'问题'字典?
答案 0 :(得分:0)
我认为你没有正确地获得问题词典。在屏幕截图中,ques
对象是NSString对象,而不是NSDictionary。因此,objectForKey
对象NSString
上的ques
无法正常运行(并且确实会导致您的应用崩溃)。
试试这个循环:
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];
}