gpt4 book ai didi

Swift 向请求添加参数

转载 作者:行者123 更新时间:2023-11-28 06:40:12 26 4
gpt4 key购买 nike

我正在尝试向 NSURLSession request 添加电子邮件和密码,如下所示:

 @IBAction func btnLogin(sender: AnyObject) {
let email = txtEmail.text
let password = txtPassword.text
let data = ["email": email!, "password": password!] as Dictionary<String, String>
let session = NSURLSession.sharedSession()
let url = NSURL(string: "http://temp.com/api/v1/login")!
let request = NSMutableURLRequest(URL: url)
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(data, options: [])
}
catch {
print("error!")
}
request.HTTPMethod = "POST"
let task = session.dataTaskWithRequest(request) {
(data, response, error) in
guard let response = response as? NSHTTPURLResponse else {print(error); return}
if response.statusCode == 200 {
print("ingelogd!")
}
self.errorLogin()
}
task.resume()
}

错误:

2016-07-03 17:54:04.890 EmployeeDirectory2[4693:133721] *** Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/Keyboard/UIKeyboardTaskQueue.m:386
2016-07-03 17:54:04.894 EmployeeDirectory2[4693:133721] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'

但是当我尝试这个时它崩溃了。如何适本地添加参数?

最佳答案

崩溃可能来自对 self.errorLogin() 调用的调用,它试图在主线程之外执行一些 UI 任务。您应该能够像这样修复它:

let task = session.dataTaskWithRequest(request) {
(data, response, error) in
guard let response = response as? NSHTTPURLResponse else {print(error); return}
if response.statusCode == 200 {
print("ingelogd!")
}
// Add this:
dispatch_async(dispatch_get_main_queue()) {
self.errorLogin()
}
}

关于Swift 向请求添加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38171701/

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