的参数列表调用dataTask这是一个项目,它包含从应用程序注册用户并注册到MySQL数据库。这在Swift 2.3中有效,但我想在Swift 3中执行,但我不知道如何解决它。将Swift 2.3转换为3错误:无法用类型为
ERROR on URLSESSIONS.shared.dataTask
代码:
else{
let url = URL(string: "http://localhost/billpain/secure/register.php")!
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
let body = "username=\(usernameTxt.text!.lowercased())&password=\(passwordTxt.text!)&email=\(emailTxt.text!)&fullname=\(firstnameTxt.text!)%20\(lastnameTxt.text!)"
request.httpBody = body.data(using: String.Encoding.utf8)
**URLSession.shared.dataTask(with: request, completionHandler: {(data:Data?, response:URLResponse?, error:NSError?) in**
if error == nil {
DispatchQueue.main.async(execute: {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
guard let parseJSON = json else {
print ("Error while parsing")
return
}
let id = parseJSON["id"]
if id != nil {
print(parseJSON)
}
} catch {
print("Caught an error: \(error)")
}
})
} else{
print("error: \(error)")
}
}).resume()
http://stackoverflow.com/questions/37812286/swift-3-urlsession-shared-ambiguous-reference-to-member-datataskwithcomplet? – Larme
为什么要指定选项'.mutableContainers',但将JSON强制转换为不可变对象? – vadian
@kkakkurt:关于您的编辑,请不要在代码格式中添加软件包的名称。 Swift和MySQL都是专有名词,而不是代码。 – halfer