gpt4 book ai didi

iOs - 无法识别的选择器发送到 TextAction 上的实例

转载 作者:行者123 更新时间:2023-11-28 17:51:04 25 4
gpt4 key购买 nike

我对这个错误有一些疑问:

ProjectName.ViewController textAction:]: unrecognized selector sent to instance 0x7fa60bc3840

当我尝试创建警报时出现错误。这是代码:

@IBAction func trySearch(sender: UIButton) {
self.api.getUser(self.textField.text!) { isResponse in
if (isResponse.count == 0) {
//Still crash HERE.
let alert = UIAlertController(title: "Error", message: "This username doesn't exist", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
else {
print("existing username")
}
}

如果我注释所有警报代码并用简单的打印替换它,它就可以工作...我真的不明白为什么...谢谢!

获取用户函数:

func getUser(user: String, completion: ((isReponse: AnyObject) -> Void)) {

let request = NSMutableURLRequest(URL: NSURL(string: "https://********")!)
request.HTTPMethod = "GET"
request.setValue("Bearer \(self.loadToken())", forHTTPHeaderField: "Authorization")

Alamofire.request(request)
.responseJSON { response in
switch response.result {
case .Success(let JSON):
completion(isReponse: JSON)
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
}

更新:当我点击我的 TextField 的“完成”时出现同样的错误。 + 我添加了 getUser 函数。

最佳答案

您可能在错误的线程中,因为您是在 block 中显示警报,请尝试:

dispatch_async(dispatch_get_main_queue(), { _ in 
let alert = UIAlertController(title: "Error", message: "This username doesn't exist", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
})

关于iOs - 无法识别的选择器发送到 TextAction 上的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36038736/

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