Using a for-loop to check input against an array - Swift

I'm attempting to make a simple application to translate English Words I have stored in an array into Spanish Words stored in another array. To accomplish this I have a Text Field for the user to enter an English word and a translate button for the user to press to get output. However, when the button is pressed I don't get any output in the label I made for output. I believe there may be something wrong with my for loop, but I'm unsure as I'm new to swift. I attempted to answer my question by reviewing Swift documentation and searching other questions. However, I was unable to find a suitable answer.

import UIKit

//Translate a word from English to Spanish.
class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var translatedWord: UILabel!

    var transIndex: Int = 0

    //Put English Words in an array
    var englishWordsArray: [String] = ["phone", "dog", "sad", "happy", "crocodile"]

    //Put Spanish words in an array
    var spanishWordsArray: [String] = ["telefono", "perro", "triste", "feliz", "cocodrilo"]

    //Close keyboard boolean
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        self.englishWord.resignFirstResponder()
        return true
    }//end resignFirstResponder

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //set delegate
        self.englishWord.delegate = self
    }// end viewDidLoad

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }//end didReceiveMemoryWarning

    //TextField for englishWord
    @IBOutlet weak var englishWord: UITextField!

    //translateButton
    @IBAction func translateButton(sender: AnyObject) {
        var word = self.englishWord.text

        //For Loop
        for var transIndex = 0; transIndex < (englishWordsArray.count); transIndex++ {
            if englishWordsArray[transIndex] == word.lowercaseString {
                print("transIndex is \(transIndex)")
            } else {
                if englishWord.text.isEmpty {
                   print("Enter an English word")
           } else {
                if englishWord != transIndex {
                    print("No translation available")
                }
            }
        }
     }
  }//end translateButton
}//end ViewController

Upvotes: 0

Views: 838

Answers (1)

Sulthan
Sulthan

Reputation: 130102

Your loop definitely doesn't work correctly. You have 3 cases:

  1. Text is empty
  2. A valid word
  3. An invalid word

Note that you don't need a for loop to test whether a text is empty. Also you need to break the loop when you have found the word. englishWord != transIndex condition is actually completely invalid (it compares a UITextField with a number).

Fixed (and without those strange comments):

var word = self.englishWord.text

if (word.isEmpty) {
   print("Enter an English word")
   return
}

for var transIndex = 0; transIndex < englishWordsArray.count; transIndex++ {
    if englishWordsArray[transIndex] == word.lowercaseString {
        print("transIndex is \(transIndex)")
        return
    }     
}

print("No translation available")

However, let's improve it

let word = self.englishWord.text

if (word.isEmpty) {
   print("Enter an English word")
   return
}

for transIndex in englishWordsArray.indices {
    if englishWordsArray[transIndex] == word.lowercaseString {
        print("transIndex is \(transIndex)")
        return
    }     
}

print("No translation available")

However, we can still use the indexOf method to replace all that

let word = self.englishWord.text

if (word.isEmpty) {
   print("Enter an English word")
   return
}

if let transIndex = englishWordsArray.indexOf(word.lowercaseString) {
    print("transIndex is \(transIndex)")
} else {
    print("No translation available")
}

Upvotes: 1

Related Questions