-1

If someone have good idea to implement this let me know thanks in adance

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    @IBOutlet weak var tableView: UITableView!

    @IBOutlet weak var tableViewCell: UITableViewCell!
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "trendingCell", for: indexPath)

        print(self.array[indexPath.row])

        cell.textLabel?.text = (self.array[indexPath.row]["title"] as! String)

        cell.detailTextLabel?.text = (self.array[indexPath.row]["username"] as! String)

        Alamofire.request(imageUrl!, method: .get).response { response in
            guard let image = UIImage(data:response.data!) else {
                // Handle error
                return
            }
            let imageData = UIImageJPEGRepresentation(image,1.0)
            cell.myImage.image = UIImage(data : imageData!)
        }

        return cell
    }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – mag_zbc Apr 25 '19 at 10:43
  • You have two force unwrap (use of "!"). Check which one is causing the crash. Soft unwrap with if let/guard let. – Larme Apr 25 '19 at 10:47

2 Answers2

0

You are force unwrapping some values. Try to safely unwrap the values instead. You still have to figure out why the values are nil though.

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

       if let url = NSURL(string: self.restaurants[indexPath.row].restaurantImage), let data = NSData(contentsOfURL: url), let image =  UIImage(data: data) {

            dispatch_async(dispatch_get_main_queue()) { () -> Void in
                    cell.restaurantImage.image = image
            }
        }
    })
Simon
  • 173
  • 7
0

This will help you figure out where the exact issue is

        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

        guard indexPath.row < self.restaurants.count
        else
        {
            print("Indexpath greater than restaurants count")
            return
        }

        guard let imageUrl =  NSURL(string:self.restaurants[indexPath.row].restaurantImage)
        else
        {
            print("Image url is empty")
            return
        }

        guard let imageData = NSData(contentsOfURL: imageUrl)
        else
        {
            print("Cannot retreive data at url")
            return
        }

        guard let image = UIImage(data: imageData)
        else
        {
            print("Data doesnt contain image")
            return
        }

        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            cell.restaurantImage.image = image
        }
    })
Sparr
  • 361
  • 1
  • 12