如何解决此问题
对成员' dataTask的不明确引用(with:completionHandler:)'
guard let requestUrl = URL(string:"http://www.digi.com/laravel_api_demo/api/demoapi") else { return }
let request = NSMutableURLRequest(url: requestUrl)
request.httpMethod = "POST"
let postString = "firstName=James&lastName=Bond"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request){ (data, response, error) in
if error != nil
{
//print(error)
return
}
//You can print out response object
print("response = \(response)")
//Print out response body
let responseString = String(data: data, encoding: NSUTF8StringEncoding)
print("response data = \(responseString)")
var err: Error?
var json = JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
var firstNameValue = parseJSON["firstName"] as? String
print("first name value = \(firstNameValue)")
}
}
task.resume()
}
答案 0 :(得分:0)
调用应该是这样的,第一个参数的类型为URL
,而不是NSMutableURLRequest
guard let requestUrl = URL(string:"http://www.digi.com/laravel_api_demo/api/demoapi") else { return }
var request = URLRequest(url: requestUrl)
request.httpMethod = "POST"
let postString = "firstName=James&lastName=Bond"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request){ (data, response, error) in
if error != nil
{
//print(error)
return
}
}
task.resume()