I am having some trouble with the return key on the keyboard executing code. I have tried in the past and the following code worked perfectly fine:
func textFieldShouldReturn(textField: UITextField) -> Bool{
textField.resignFirstResponder()
valueOfLetter()
return true;
}
But, for some reason, there is an error on the line valueOfLetter
.
Here is the entire file in case it is 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
}
}
The error is: "missing argument in parameter #1 in call
The is another error on the line for (index, letter) in alphabet
which says: 'String' not convertible to '([String, String])'
I am unsure of what these errors mean or how to fix them.
Any input or suggestions would be greatly appreciated.
Thanks in advance.
Your method valueOfLetter
takes a parameter, you need to call it with a parameter or it won't work. For instance:
valueOfLetter("x")
Your second error is occurring because you're using a syntactic structure used to iterate over a dictionary with an array. Use the built-in enumerate
function to iterate over an array with index values:
for (index, letter) in enumerate(alphabet) {
if letter = inputLetter.lowercaseString {
return index + 1
for character in word {
score += valueOfLetter(character)
}
}
}
The error message is because your [String]
array cannot be fast-enumerated as a pair of indices and values. To do so, you need to use the enumerate
function like mentioned above.
I would recommend promoting your alphabet
array to a global or member variable. You can mark it as private so that it is not visible outside that source file.
// 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"]
You can use two functions to compute the score for a word. The first function will calculate the number of points for a specific letter. The second function will take a string and enumerate through it, summing up the point values for each letter.
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) }
}
I know this is a bit different than your initial setup. I just wanted to point out how useful the built-in Swift library functions are. Namely filter
, map
, and reduce
.
EDIT: Here is how you can use each method via 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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With