0

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
2
  • For loops of that sort will soon be expunged from Swift anyway, so best to avoid them and learn to loop the Swift way. Commented Feb 17, 2016 at 4:30
  • Any change you would be able to provide a link to the swift way or a quick example? Commented Feb 17, 2016 at 4:34

1 Answer 1

1

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")
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! I'm giving this a try now. I'll have to look into how to use indices correctly too as that looks so much cleaner. I'll set it as the answer once I get it working.
If I use the indexOf method don't I need to call that method somewhere else in my code to get it to run? I'm getting a compile error with the 2nd and 3rd block of code. I'm able to get the first block of code to work, however seems to not want to put it in my label. I'll have to take another look at my labels and make sure I have it connected properly.
@BrandonZink Which version of Xcode are you using? I have tested both versions and they work. However, if you are using an older Xcode version (Swift 1.x) then they won't work because the language evolved. If you are sure you are using Xcode 7, please add the compile message and I will try to help you.
Well, you should upgrade Xcode to 7.1 because Swift has changed a lot since Xcode 6. In Xcode 6 the third version would be if let transIndex = find(englishWordsArray, word.lowercaseString) {.
I'm definitely going to update to 7.1 Thanks for the help!. I should be able to get it working from here. I really appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.