斯威夫特 – 我怎么让文本字段仍然识别正确的答案后input正确的答案空间

我在有文本字段的地方进行测验,用户input答案。 但是,我希望它能够认识到正确答案后的空格仍然正确。 例如,如果答案是红色,并且用户在红色之后input一个空格,那么它仍然被认为是正确的答案。 以下是我的代码。 请让我知道,并提前感谢您的所有帮助!

import Foundation import UIKit class Case1Q3ViewController: UIViewController, UITextFieldDelegate { @IBOutlet weak var nextButton: UIButton! @IBOutlet var questionLabel: UILabel! @IBOutlet var correctAnswerLabel: UILabel! @IBOutlet var inputTextField: UITextField! var enteredAnswer: String? var correctAnswer = "Well demarcated" override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(InputViewController1.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(InputViewController1.keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil) inputTextField.delegate = self titlesForLabels() nextButton.enabled = false } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func titlesForLabels() { questionLabel.text = "Describe the borders" correctAnswerLabel.text = correctAnswer correctAnswerLabel.hidden = true inputTextField.text = nil inputTextField.enabled = true } func keyboardWillShow(notification: NSNotification) { let userInfo = notification.userInfo! let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() UIView.animateWithDuration(0.1, animations: { () -> Void in self.view.frame.origin.y = -keyboardFrame.size.height }) } func keyboardWillHide() { UIView.animateWithDuration(0.1, animations: { () -> Void in self.view.frame.origin.y = 0 }) } func textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder() enteredAnswer = textField.text checkForCorrectAnswer() return true } func checkForCorrectAnswer() { if enteredAnswer!.lowercaseString == correctAnswer.lowercaseString { print("Correct") correctAnswerLabel.textColor = UIColor.greenColor() correctAnswerLabel.text = "Correct!" nextButton.enabled = true } else { print("Wrong Answer") correctAnswerLabel.textColor = UIColor.redColor() correctAnswerLabel.text = "Incorrect! Please try again" nextButton.enabled = false } correctAnswerLabel.hidden = false } override func viewWillAppear(animated: Bool) { navigationItem.title = "Case 1 - Q3" } } 

您应该在检查相等之前修剪空白。 你可以使用stringByTrimmingCharactersInSet:方法来达到这个目的。

 func checkForCorrectAnswer() { let answer = enteredAnswer!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) if answer.lowercaseString == correctAnswer.lowercaseString { print("Correct") correctAnswerLabel.textColor = UIColor.greenColor() correctAnswerLabel.text = "Correct!" nextButton.enabled = true } else { print("Wrong Answer") correctAnswerLabel.textColor = UIColor.redColor() correctAnswerLabel.text = "Incorrect! Please try again" nextButton.enabled = false } correctAnswerLabel.hidden = false } 

你可以使用这样的东西:

 let str = "Hi How Are You" let newString = str.stringByReplacingOccurrencesOfString(" ", withString: "") print(newString) //This prints "HiHowAreYou" 

然后只是比较用户答案和正确的答案都没有空格。 在你的情况下,你会使用这样的东西:

 if enteredAnswer.stringByReplacingOccurrencesOfString(" ", withString: "").lowercaseString == correctAnswer.stringByReplacingOccurrencesOfString(" ", withString: "").lowercaseString { //Answer Is Correct 

}