Search code examples
iosswiftimageurlnsurlrequest

Swift 4 Error: "Cannot convert value of type 'URLRequest' to expected argument type 'URL!'"


Just what the title says. I am getting this error thrown and not sure if this has to do with the Swift 4 conversion. I have been trying to work around the code but no luck yet so decided to throw this question up on SO. This error is getting thrown on line: "cell.businessPostImage.setImageWith(postRequest,". Thanks.

cell.businessPostImage.image = nil
        if let postURL = URL(string: downloadURL) {
            let postRequest = URLRequest(url: postURL)
            cell.businessPostImage.setImageWith(postRequest, placeholderImage: nil, options:
            { (imageRequest, imageResponse, image) in
                    cell.businessPostImage.contentMode = UIViewContentMode.scaleToFill
                    cell.businessPostImage.image = image
            }, completed: { (imageRequest, imageResponse, error) -> Void in
                // failure downloading image
                print("Error downloading Firebase post image")
                print(error)
            })
        }

This is the error its getting thrown WITH the edit


Solution

  • You have to use like this :

    if let postURL = URL(string: downloadURL) {
        let postRequest = URLRequest(url: postURL)
        cell.businessPostImage.setImageWith(postURL, placeholderImage: nil, options: SDWebImageOptions.ProgressiveDownload, completed: { (imageRequest, imageResponse, error) -> Void in
            // failure downloading image
            print("Error downloading Firebase post image")
            print(error)
        })
    }