Q: Fatal error when scroll down i am passing json in tableView, but it show the message "fatal error: unexpectedly found nil while unwrapping an Optional value" when i am scrolling down a few times
let imageCache = NSCache<AnyObject, AnyObject>()

class CustomImageView: UIImageView {

    var imageUrlString: String?

    func loadImageUsingUrlString(_ urlString: String) {

        imageUrlString = urlString

         let urlEncoded = urlString.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)

        let url = URL(string: urlEncoded!)

        image = nil

        if let imageFromCache = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
            self.image = imageFromCache
            return
        }


        // crush here down line
        URLSession.shared.dataTask(with: url!, completionHandler: {(myData, respones, error) in

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

            DispatchQueue.main.async(execute: {

                let imageToCache = UIImage(data: myData!)

                if self.imageUrlString == urlString {
                    self.image = imageToCache
                }

                imageCache.setObject(imageToCache!, forKey: urlString as AnyObject)
            })

        }).resume()
    }

}



A: Enclose your URLSession.shared.dataTask(with:completionHandler:) in a if-let clause like this:
if let url = url {
    URLSession.shared.dataTask(with: url, completionHandler: {(myData, respones, error) in

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

        DispatchQueue.main.async(execute: {

            let imageToCache = UIImage(data: myData!)

            if self.imageUrlString == urlString {
                self.image = imageToCache
            }

            imageCache.setObject(imageToCache!, forKey: urlString as AnyObject)
        })

    }).resume()
}

Note: And also try not to use force-unwrapping (!) as long as possible. Instead use optional binding with if-let everywhere possible.

Edit
As you might be new with optionals I'm posting the possible crash handled code. Though I didn't change the logics, I fixed possible crash related code:
let imageCache = NSCache<AnyObject, AnyObject>()

class CustomImageView: UIImageView {

    var imageUrlString: String?

    func loadImageUsingUrlString(_ urlString: String) {

        imageUrlString = urlString

        guard let urlEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
            print("Encoding not done")
            return
        }

        let url = URL(string: urlEncoded)

        image = nil

        if let imageFromCache = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
            self.image = imageFromCache
            return
        }


        if let url = url {
            URLSession.shared.dataTask(with: url, completionHandler: {(myData, respones, error) in

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

                if let myData = myData {
                    DispatchQueue.main.async(execute: {

                        let imageToCache = UIImage(data: myData)

                        if self.imageUrlString == urlString {
                            self.image = imageToCache
                        }

                        if let imageToCache = imageToCache {
                            imageCache.setObject(imageToCache, forKey: urlString as AnyObject)
                        }
                    })
                }

            }).resume()
        }
    }

}


A: You need to check url before use
For that you can use this two options
1) if let
2) guard let (Better)
i.e
Here Replace this code
    let url = URL(string: urlEncoded!)

with 
 guard let  url = URL(string: urlEncoded) else {
    print("Your Url is nil")
     return
 }



Before you forcefully unwarp you must sure about value otherwise put it in if let or guard let block 


And also use guard let at  here too
    guard  let imageFromCache = imageCache.object(forKey: urlString as AnyObject) as? UIImage else {
     return
    }

It makes your code easy to read
Hope it helps to you

A: might be url is nil so try this code.
if url.absoluteString != "" {

 URLSession.shared.dataTask(with: url, completionHandler: {(myData, respones, error) in

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

                DispatchQueue.main.async(execute: {

                    let imageToCache = UIImage(data: myData!)

                    if self.imageUrlString == urlString {
                        self.image = imageToCache
                    }

                    imageCache.setObject(imageToCache!, forKey: urlString as AnyObject)
                })

            }).resume()
        }

