ios - pattern.firstMatch 得到错误:表达式类型“@lvalue 字符串?” 没有更多上下文是模棱两可的
问题描述
我是第一个 iPhone 开发者。我想要做的是区分文本字段中的特殊字符。所以我尝试使用正则表达式。但它向我显示了错误。
@IBOutlet weak var walletNameField: UITextField!
@IBAction func nextButtonFuc(_ sender: UIButton) {
let pattern = try! NSRegularExpression(pattern: "^[a-zA-Z0-9가-힣ㄱ-하-ㅣ\\s]$", options: .caseInsensitive)
if walletNameField.text!.isEmpty {
callAlert("test")
} else if walletPasswordField.text!.isEmpty {
callAlert("test")
} else if confirmField.text!.isEmpty {
callAlert("test")
} else if walletPasswordField.text != confirmField.text {
callAlert("test")
} else if walletNameField.text!.count > 10 {
callAlert("test")
} else if walletPasswordField.text!.count < 8 ||
walletPasswordField.text!.count >= 20 {
callAlert("test")
} else if pattern.firstMatch(in: "\(walletNameField.text!)", options: NSRegularExpression.MatchingOptions.reportCompletion, range: NSMakeRange(0, walletNameField.text!.count)) { //get Error Expression type '@lvalue String?' is ambiguous without more context
callAlert("test")
}
}
错误是
表达式类型“@lvalue 字符串?” 没有更多上下文是模棱两可的
我做错了什么?我觉得很难解决这个问题。
当我触摸任何地方时,我都会尝试隐藏我的键盘。但是,函数本身是一个错误。为什么会出现错误?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
// Error is Declaration 'touchesBegan(touches:withEvent:)' has different argument labels from any potential overrides
提前致谢
解决方案
您需要firstMatch
与预期的值进行比较if
A BOOL
} else if pattern.firstMatch(in: "\(walletNameField.text!)", options: NSRegularExpression.MatchingOptions.reportCompletion,range: NSRange(location:0,length:walletNameField.text!.count)) != nil {
}
或使用if let
} else if let first = pattern.firstMatch(in: "\(walletNameField.text!)", options: NSRegularExpression.MatchingOptions.reportCompletion,range: NSRange(location:0,length:walletNameField.text!.count)) {
print(first)
}
升级touchesBegan
你需要在这里使用最新的语法
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
推荐阅读
- functional-programming - `is pure` trait 和默认参数
- c# - WebView2 - 此站点正在尝试打开(应用程序)
- matrix - 反向传播中的维度不匹配
- android - 如何在 API 级别 17 中为 OkHttpClient.Builder() 启用 Tls12
- javascript - 可以使用 React Spring 在更多已加载的图像之间切换吗?
- sql-server - 在 SQL Server 中解析电子邮件地址
- r - 如何通过按下 Shiny 中的提取按钮来运行应用程序
- php - php函数限制mysql查询结果
- excel - 循环二维数组不显示保存的值
- django - Django Heroku:向heroku添加密钥不起作用