SwiftDeveloper SwiftDeveloper - 1 year ago 5583
JSON Question

Swift 3 URLSession.shared() Ambiguous reference to member 'dataTask(with:completionHandler:) error (bug)

Hello I have working json parsing code for swift2.2 but when i use it for Swift 3.0 gives me that error

ViewController.swift:132:31: Ambiguous reference to member 'dataTask(with:completionHandler:)'

My codes here

let listUrlString = "http://bla.com?batchSize=" + String(batchSize) + "&fromIndex=" + String(fromIndex)
let myUrl = URL(string: listUrlString);
let request = NSMutableURLRequest(url:myUrl!);
request.httpMethod = "GET";

let task = URLSession.shared().dataTask(with: request) {
data, response, error in

if error != nil {
DispatchQueue.main.sync(execute: {


do {

let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray

if let parseJSON = json {

var items = self.categoryList

items.append(contentsOf: parseJSON as! [String])

if self.fromIndex < items.count {

self.categoryList = items
self.fromIndex = items.count

DispatchQueue.main.async(execute: {



}else if( self.fromIndex == items.count){

DispatchQueue.main.async(execute: {





} catch {



Thanks for ideas.

Answer Source

The compiler is confused by the function signature - I suppose this is a bug that will be resolved very soon.

In the meantime, write it like this:

let task = URLSession.shared().dataTask(with: request as URLRequest) {


Note that we don't have to cast "request" as URLRequest in this signature if it was declared earlier as URLRequest instead of NSMutableURLRequest. This is the automatic casting between NSMutableURLRequest and the new URLRequest that is failing and which forces us to do this casting here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download