Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试猜谜游戏。在 0 和 5 之间猜测。我知道我的大多数问题都可以使用数字键盘进行数据输入排序,但我正在尝试尽可能多地学习,所以我正在尝试考虑所有可能发生的不正确错误使用全键盘并给出相关的错误信息。
我想我已经涵盖了所有内容,唯一我无法得到的是在输入字符而不是数字时出错。有人可以帮忙吗。我的代码是:
在此处输入图像描述
谢谢
你可以做一个数字1-5的数组
let numbers: [String] = ["1", "2", "3", "4", "5"]
然后您可以检查文本字段输入是否有效
if textfieldname.text.count == 1 { if numbers.contains(textfieldname.text) { //do something } else { //oops... } else { //too many characters }