Problems with keyboard features?

I am having problems with the return key on a keyboard executing code. I tried in the past and the following code worked perfectly:

func textFieldShouldReturn(textField: UITextField) -> Bool{
    textField.resignFirstResponder()
    valueOfLetter()
    return true;
}

But for some reason valueOfLetterthere is an error on the line .

Here is the whole file if necessary:

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

@IBOutlet weak var strWordValue: UILabel!
@IBOutlet weak var strInputField: UITextField!


func textFieldShouldReturn(textField: UITextField) -> Bool{
    textField.resignFirstResponder()
    valueOfLetter()
    return true;
}

 var TextField: UITextField!

    func valueOfLetter(inputLetter: String) -> Int {
        let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

        for (index, letter) in alphabet {
            if letter = inputLetter.lowercaseString {
                return index + 1

                for character in word {
                    score += valueOfLetter(character)
                }
            }
        }

        return 0
    }
} 

Error: "Missing argument in parameter No. 1 when called

Another error in a string for (index, letter) in alphabetthat states: 'String' does not convert to '([String, String])'

I'm not sure what these errors mean or how to fix them.

Any materials or suggestions are welcome.

Thanks in advance.

+4
source share
2 answers

, [String] . enumerate, .

alphabet -. , .

// A through Z
private let alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

. . , .

func valueOfLetter(letter: Character) -> Int
{
    let letterString = String(letter).uppercaseString
    let index = find(alphabet, letterString)

    return index != nil ? index! + 1 : 0
}

func scoreForWord(word: String) -> Int
{
    let characters = Array(word)
    return characters.reduce(0) { sum, letter in sum + valueOfLetter(letter) }
}

, . , Swift. filter, map reduce.

EDIT: UITextFieldDelegate:

// This will be called every time the text changes in the UITextField
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    let currentWord = textField.text as NSString
    let newWord = currentWord.stringByReplacingCharactersInRange(range, withString: string)

    let score = scoreForWord(newWord)

    // Do something with the score here

    return true
}

// This will be called when the return key is pressed
func textFieldShouldReturn(textField: UITextField) -> Bool
{
    let word = textField.text
    let score = scoreForWord(word)

    // Do something with the score here

    return true
}
+3

valueOfLetter , . :

 valueOfLetter("x")

, , . enumerate :

   for (index, letter) in enumerate(alphabet) {
        if letter = inputLetter.lowercaseString {
            return index + 1

            for character in word {
                score += valueOfLetter(character)
            }
        }
    }
+8

All Articles