2017-04-07 80 views
0

的参数列表调用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() 
+0

http://stackoverflow.com/questions/37812286/swift-3-urlsession-shared-ambiguous-reference-to-member-datataskwithcomplet? – Larme

+1

为什么要指定选项'.mutableContainers',但将JSON强制转换为不可变对象? – vadian

+0

@kkakkurt:关于您的编辑,请不要在代码格式中添加软件包的名称。 Swift和MySQL都是专有名词,而不是代码。 – halfer

回答

0
let url = URL(string: "http://localhost/billpain/secure/register.php")! 
var urlRequest = URLRequest(url: url) 
urlRequest.httpMethod = "POST" 
let body = "username=\(usernameTxt.text!.lowercased())&password=\(passwordTxt.text!)&email=\(emailTxt.text!)&fullname=\(firstnameTxt.text!)%20\(lastnameTxt.text!)" 
urlRequest.httpBody = body.data(using: String.Encoding.utf8) 

let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in 
if let error = error { 
    print("error:", error) 
    return 
} 

do { 
    guard let data = data else { return } 
    guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: AnyObject] 
else { return } 
    print("json:", json) 
} catch { 
    print("error:", error) 
} 
}task.resume() 
+0

}) }其他{ 打印。( “错误:\(错误)”) } })恢复() }。有关连续语句的错误 – amr07

+0

.task.resume()错误:使用未解析的标识符'任务' – amr07

+0

检查编辑答案 –

相关问题