我是第一位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 String?'没有更多上下文就变得模棱两可
我做错了什么?我发现很难解决这个问题。
当我触摸任何地方时,我都会尝试隐藏键盘。但是,函数本身是错误。为什么会出现错误?
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
预先感谢
答案 0 :(得分:2)
您需要将firstMatch
与一个值进行比较,因为if
需要一个布尔值
} 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
,您需要使用最新的语法Here
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
答案 1 :(得分:0)
第二个问题,即隐藏键盘,该功能应为:-
override func touchesBegan(touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}