gpt4 book ai didi

ios - pattern.firstMatch 得到错误 : Expression type '@lvalue String?' is ambiguous without more context

转载 作者:行者123 更新时间:2023-11-28 11:27:15 28 4
gpt4 key购买 nike

我是第一个 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")
}
}

错误是

Expression type '@lvalue String?' is ambiguous without more context

我做错了什么?我觉得很难解决这个问题。

当我触摸任何地方时,我会尽量隐藏我的键盘。但是,函数本身是错误的。为什么会出错?

    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 需要一个 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 你需要使用最新的语法 Here

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}

关于ios - pattern.firstMatch 得到错误 : Expression type '@lvalue String?' is ambiguous without more context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57837399/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com