0

I get this error:

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

See code where it happens (added comments below the error line)

{
    for metadata in metadataObjects{
        let decodedData:AVMetadataMachineReadableCodeObject = metadata as! AVMetadataMachineReadableCodeObject

        if currentISBN == decodedData.stringValue { continue; }

        currentISBN = decodedData.stringValue

        //self.lblDataInfo.text = decodedData.stringValue
        //self.lblDataType.text = decodedData.type

        let url = URL(string: "https://www.googleapis.com/books/v1/volumes?q=isbn:"+decodedData.stringValue+"&key=AIzaSyAf9fhYZLHjjqfWiTXZeSikNTZOt5yNwoU")
        let session = URLSession.shared
        let request = NSMutableURLRequest(url: url!)
        request.httpMethod = "GET"
        request.addValue(Bundle.main.bundleIdentifier ?? "", forHTTPHeaderField: "X-Ios-Bundle-Identifier")

        //HERE I GET THE ERROR ON THIS LINE:
        **let task = session.dataTask(with: request, completionHandler: { (data, response, error) -> Void in**

            if error != nil {
                print(error)
                return
            }

            do {
                //let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary

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

                if json is [String: AnyObject] {
                    print(json)
                    if let error = json["error"] as? String {
                        print(error);
                    } else if let items = json["items"] as? [[String: AnyObject]] {

                        for item in items {
                            print(item)

                            let book_id = item["id"] as? String

                            if let volumeInfo = item["volumeInfo"] as? [String: AnyObject] {
                                let book_title = volumeInfo["title"] as? String
                                DispatchQueue.main.async(execute: { () -> Void in
                                    self.lblDataInfo.text = "ISBN: "+self.currentISBN!+"  ID:"+book_id!
                                    self.lblDataType.text = book_title
                                })
                            }

                            break // for now, only show first
                        }

                    } else {
                        DispatchQueue.main.async(execute: { () -> Void in
                            self.lblDataInfo.text = "ISBN: "+self.currentISBN!+"  Not identified"
                            self.lblDataType.text = ""
                        })
                    }
                }

            } catch let jsonError {
                print(jsonError)
            }

        })
        task.resume()

    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Das Mupp
  • 49
  • 5
  • Google is your friend. **dataTask Ambiguous reference to member 'dataTask(with:completionHandler:)' error** top hit would be the linked question. You should save SO bandwidth doing that https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwjtzKzA9IHXAhVDi5AKHbjqBzQQFggmMAA&url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F37812286%2Fswift-3-urlsession-shared-ambiguous-reference-to-member-datataskwithcomplet%2F39967529&usg=AOvVaw0Gv1S0r4DG3QnTKHHyc94V – Leo Dabus Oct 21 '17 at 14:21

1 Answers1

0

The method expects native URLRequest, not NSMutableURLRequest.

var request = URLRequest(url: url!)

And a JSON dictionary in Swift 3+ is [String:Any] not [String:AnyObject]

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Thank you that fixed the problem! But now I get error (Type 'Any' has no subscript members) on this line instead: `if json is [String:Any] { print(json) if let error = json["error"] as? String { print(error); } else if let items = json["items"] as? [[String:Any]] {` – Das Mupp Oct 21 '17 at 13:55
  • @vadian this should be marked as duplicate. You have enough reputation for that. Even a single vote of you (Swift gold badge) would close it as such – Leo Dabus Oct 21 '17 at 13:58