Sujay U N
Sujay U N

Reputation: 5340

Ios Swift3 : URLSession shared datatask showing an error

I am getting the below error while trying to get data from PHP sever.
URLSession being initialised seems to have an error at shared.
Kindly help me to get swift3 equivalent.

This is error message and sample code below :

Ambiguous reference to member 'dataTask(with:completionHandler:)'

let task = URLSession.shared.dataTask(with: PhpReqVar)
{
    data, response, error in
    // Code Todo
}

Thanks in advance.

Upvotes: 1

Views: 427

Answers (2)

dirtydanee
dirtydanee

Reputation: 6151

The code should be the following:

let task = URLSession.shared.dataTask(with: PhpReqVar as URLRequest) { (data, response, error) in
 //code here    
}

You have missed to add the completionHandler's parameters. Also specifying PhpReqVar as URLRequest, not as MutableURLRequest.

Upvotes: 1

FelixSFD
FelixSFD

Reputation: 6092

Your completion handler does not take any parameters and you are not closing the (.

Try this (assuming that PhpReqVar is of type URL or URLRequest):

let task = URLSession.shared.dataTask(with: PhpReqVar)
{
    data, response, error in
    //CodeTodo
}

Upvotes: 1

Related Questions