简体   繁体   English

比较字符串数组和按钮单击

[英]compare array of strings to a button click

I am trying to learn new stuff by creating quiz app 我正在尝试通过创建测验应用程序来学习新知识

I have created array of question and answer 我创建了一系列的问题和答案

but answer are in only "Yes" or "No". 但是答案只有“是”或“否”。

I am able to create question to appear randomly but i am struck how to confirm or get if the particluar question appearing on screen have solution yes or no 我能够创建随机出现的问题,但是我很惊讶如何确认或获取屏幕上出现的特定问题是否具有解决方案“是”或“否”

It is very simple app, just want to confirm if the selected question real answer is yes or no? 这是一个非常简单的应用程序,只想确认所选问题的真实答案是是还是否?

there are several method to do this simple task. 有几种方法可以完成此简单任务。

one is make dictionary with keys (Question, Answer) and add to array as 一种是用键(问题,答案)制作字典,然后将其添加到数组中

NSArray *questions = @[@{@"Question":@"Question 1 Text", @"Answer":@YES},
                       @{@"Question":@"Question 1 Text", @"Answer":@NO}]; 

And Generate a random number and get the question dictionary from array and save the reference of question dictionary for further comparison. 并生成一个随机数并从数组中获取问题字典,并保存问题字典的引用以进行进一步比较。

NSDictionary *curQuestion = questions[(rand()%questions.count)];
NSString *question = curQuestion[@"Question"];
BOOL answer = [curQuestion[@"Answer"] boolValue];

创建一个字典数组(带有两个键“ Question”和“ Answer”的字典),当一个问题被回答时,您可以从数组访问相应的字典,然后获得该问题的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM