使用for循环检查数组的输入 - Swift

时间:2016-02-17 04:23:03

标签: arrays swift for-loop

我试图制作一个简单的应用程序,将我存储在数组中的英语单词翻译成存储在另一个数组中的西班牙语单词。为了实现这一点,我有一个文本字段供用户输入英文单词和翻译按钮,供用户按下以获得输出。但是,当按下按钮时,我没有在输出的标签中获得任何输出。我相信我的for循环可能有问题,但我不确定我是不是很快。我试图通过查看Swift文档和搜索其他问题来回答我的问题。但是,我无法找到合适的答案。

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

1 个答案:

答案 0 :(得分:1)

你的循环肯定无法正常工作。你有3个案例:

  1. 文字为空
  2. 有效字
  3. 无效字
  4. 请注意,您不需要for循环来测试文本是否为空。当你找到这个词时,你还需要break循环。 englishWord != transIndex条件实际上完全无效(它将UITextField与数字进行比较)。

    修正(没有那些奇怪的评论):

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

    但是,让我们改进它

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

    但是,我们仍然可以使用indexOf方法替换所有

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