0

so having some issues with creating a URLSession in Swift 3 referencing the dataTask. I am getting this error and do not understand why:

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

func fetchVideos() {

    let url = NSURL(string: "https://s3-us-west-2.amazonaws.com/youtubeassets/home.json")
    URLSession.shared.dataTask(with: url!) { (data, response, error) in

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

        let str = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print(str)

    // this resume function is not reading correctly either
    }.resume()

}
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
user3708224
  • 1,229
  • 4
  • 19
  • 37

1 Answers1

3

In Swift 3, you should not use the NS-prefixed types, when there is a native Swift type available. For example, instead of NSURL, use URL, and instead of Objective-C's NSString, use String. Generally, try to use Swift-Foundation as much as possible.

let url = URL(string: "https://s3-us-west-2.amazonaws.com/youtubeassets/home.json")
URLSession.shared.dataTask(with: url!) { (data, response, error) in

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

    let str = String(data: data!, encoding: .utf8)
    print(str)

}.resume()
FelixSFD
  • 6,052
  • 10
  • 43
  • 117