加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

键盘功能有问题?

发布时间:2020-12-14 04:50:01 所属栏目:百科 来源:网络整理
导读:我在执行代码的键盘上的返回键有问题.我在过去尝试过以下代码完美无缺: func textFieldShouldReturn(textField: UITextField) - Bool{ textField.resignFirstResponder() valueOfLetter() return true;} 但是,由于某种原因,行valueOfLetter上存在错误. 如果
我在执行代码的键盘上的返回键有问题.我在过去尝试过以下代码完美无缺:

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

但是,由于某种原因,行valueOfLetter上存在错误.

如果有必要,这是整个文件:

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

错误是:“在调用中参数#1中缺少参数

这是字母表中(index,letter)行的另一个错误:’String’不能转换为'([String,String])’

我不确定这些错误是什么意思或如何解决它们.

任何意见或建议将不胜感激.

提前致谢.

解决方法

错误消息是因为您的[String]数组不能快速枚举为一对索引和值.为此,您需要使用上面提到的枚举函数.

我建议将字母数组推广到全局变量或成员变量.您可以将其标记为私有,以便在源文件外部不可见.

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

我知道这与你的初始设置有点不同.我只是想指出built-in Swift library functions are的有用性.即过滤,映射和减少.

编辑:以下是如何通过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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读