Babz Babz - 5 months ago 18
Swift Question

Swift - how do I allow the textfield to still recognize spaces after the correct answer inputed as still the correct answer

I am making a quiz where there is a textfield and the user enters an answer. However, I want it to recognize that a space after the correct answer is still correct. For example if the answer is Red and the user enters Red with a space after Red, it is still recognized as the correct answer. Below is my code. Please let me know and thanks for all your help in advance!

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"
}
}

Answer

You should trim the whitespace before checking the equality. You can use the stringByTrimmingCharactersInSet: method for this purpose.

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
}