Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用swift检查文本字段的答案?_Swift_String_Xcode - Fatal编程技术网

如何使用swift检查文本字段的答案?

如何使用swift检查文本字段的答案?,swift,string,xcode,Swift,String,Xcode,我正在用一个页面在Xcode中编写一个基本的测验应用程序,需要关于如何检查答案的帮助。我有一系列的问题和答案。当用户在文本字段中输入答案时,我如何检查答案是否正确 @IBOutlet var questionLabel:UILabel! @IBOutlet var answerField:UITextField! @IBOutlet var instructionsLabel:UILabel! let questions:[String] = ["In what country is the

我正在用一个页面在Xcode中编写一个基本的测验应用程序,需要关于如何检查答案的帮助。我有一系列的问题和答案。当用户在文本字段中输入答案时,我如何检查答案是否正确

@IBOutlet var questionLabel:UILabel!
@IBOutlet var answerField:UITextField!
@IBOutlet var instructionsLabel:UILabel!


let questions:[String] = ["In what country is the Christ the Redeemer statue located in?", "In what country is Machu Picchu lacated in?", "In what country is the Taj Mahal located in?", "In what country is the Great Pyramids of Giza located in?","In what country is Petra located in?","In what country is the Great Wall located in?", "In what country is the ruins if Chichen Itza located in?"]
let answers:[String] = ["Brazil", "Peru", "India", "Egypt", "Jordan", "China", "Mexico"]


var currentQuestionIndex:Int = 0

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    currentQuestionIndex = questions.count - 1
}
@IBAction func showNextQuestion(sender:AnyObject){

    if(currentQuestionIndex<questions.count-1) {
        currentQuestionIndex = currentQuestionIndex + 1
    } else {
        currentQuestionIndex = 0
    }
    questionLabel.text = questions[currentQuestionIndex]

}

@IBAction func checkAnswer(){

}

}
@IBOutlet var questionLabel:UILabel!
@IBOutlet var应答字段:UITextField!
@IBOUTLE var指令标签:UILabel!
让我们提问:[字符串]=[“救世主雕像位于哪个国家?”,“马丘比丘位于哪个国家?”,“泰姬陵位于哪个国家?”,“吉萨大金字塔位于哪个国家?”,“佩特拉位于哪个国家?”,“长城位于哪个国家?”,“如果奇琴伊萨位于哪个国家?”
让我们回答:[字符串]=[“巴西”、“秘鲁”、“印度”、“埃及”、“约旦”、“中国”、“墨西哥”]
var currentQuestionIndex:Int=0
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
currentQuestionIndex=questions.count-1
}
@iAction func showNextQuestion(发件人:AnyObject){

如果(currentQuestionIndex,这里有一种检查答案的方法

@IBAction func checkAnswer() {
    guard let answer = answerField.text, !answer.isEmpty else {
        //answer is empty
        return
    }

    if answers[currentQuestionIndex].lowercased() == answer.lowercased() {
        //correct answer found
    } else {
        //Incorrect answer
    }
}

请注意,我没有编写任何代码来向用户显示结果,但您可以使用一个标签,在其中您可以编写一条消息,而我刚刚写了一条评论。更高级的解决方案可以是将结果消息显示为警报或工作表。

您可以显示失败的尝试吗?这样,我可以看到您的误解丁是,或者什么概念你不懂。你可以问你的问题。你需要一个到你的文本领域的出口。我有一个刚刚忘记发布。对不起,这一切都是新的。