1

I'm having a problem with my code. I am trying to download a picture through a StringBuilder and then set it to a UIImage I seem to get a problem and I hope someone can see what I have done wrong.

setUI:

uiMovieTitle.text = self.movies![movieIndex].title

    var finalImageUrl = StringBuilder()

    let session = URLSession(configuration: .default)

    let downloadPicTask = session.dataTask(with: finalImageUrl) { (data, response, error) in
        // The download has finished.
        if let e = error {
            print("Error downloading cat picture: \(e)")
        } else {
            // No errors found.
            // It would be weird if we didn't have a response, so check for that too.
            if let res = response as? HTTPURLResponse {
                print("Downloaded cat picture with response code \(res.statusCode)")
                if let imageData = data {
                    // Finally convert that Data into an image and do what you wish with it.
                    let image = UIImage(data: imageData)
                    // Do something with your image.
                    uiMoviePoster.image = image
                } else {
                    print("Couldn't get image: Image is nil")
                }
            } else {
                print("Couldn't get response code for some reason")
            }
        }
    }

   downloadPicTask.resume()

}

StringBuilder

func StringBuilder() -> (String){
    let posterBase = "http://image.tmdb.org/t/p/w1920"
    let linkEnd = self.movies?[movieIndex].posterPath

    var finalLink = ""

    finalLink = posterBase + linkEnd!

    return finalLink

}

I do also have another download which gets me a list of movies(JSON) and is crucial for the StringBuilder.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Pr0tonion
  • 187
  • 2
  • 12
  • 1
    StringBuilder should return a URL or a URLRequest – Leo Dabus Jan 21 '17 at 14:41
  • Possible duplicate of [Swift 3 URLSession.shared() Ambiguous reference to member 'dataTask(with:completionHandler:) error (bug)](http://stackoverflow.com/questions/37812286/swift-3-urlsession-shared-ambiguous-reference-to-member-datataskwithcomplet) – shallowThought Jan 21 '17 at 15:37
  • I think your `finalLink` is missing a slash `/`. Fix it or better use `URL`. – shallowThought Jan 21 '17 at 15:43

1 Answers1

0

The compiler is complaining because the function SwiftBuilder returns a String and there are multiple methods on URLSession named dataTask(with:completion:), but none of them take a String for the first argument.

If you need SwiftBuilder to continue to return a string for some other part of your code, then for here you'll need to convert that string to a URL.

Something like the following should work:

let session = URLSession(configuration: .default)

let imageUrlString = StringBuilder()
if let imageUrl = URL(string: imageUrlString) {
let downloadPicTask = session.dataTask(with: imageUrl) { (data, response, error) in
    // The download has finished.

And so on... let me know if that makes sense.

Dave Weston
  • 6,527
  • 1
  • 29
  • 44