1

I am getting

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

at DispatchQueue.main.async{ self.tableView.reloadData() }

My code

import UIKit

class BrowseRequestsViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {

    final let url = URL(string: "")

    private var browseRequestDataModel = [Datum]()

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        downloadJson()
    }

    func downloadJson() {
        guard let downloadURL = url else { return }
        URLSession.shared.dataTask(with: downloadURL) { data, urlResponse, error in
            guard let data = data, error == nil, urlResponse != nil else {
                print("something is wrong")
                return
            }
            print("downloaded")
            do {
                let decoder = JSONDecoder()
                let downloadedBrowseRequestData = try decoder.decode(BrowseRequestsDataModel.self, from: data)
                self.browseRequestDataModel = downloadedBrowseRequestData.data
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            } catch {
                print("something wrong after downloaded")
            }
        }.resume()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return browseRequestDataModel.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "BrowseRequestsTableViewCell" ) as? BrowseRequestsTableViewCell else { return UITableViewCell() }

        cell.shipperName.text = browseRequestDataModel[indexPath.row].user.name
        cell.pickupLocation.text = browseRequestDataModel[indexPath.row].pickupLocation.pickupLocationCity + " , " + browseRequestDataModel[indexPath.row].pickupLocation.pickupLocationCountry
        cell.dropoffLocation.text = browseRequestDataModel[indexPath.row].dropoffLocation.dropoffLocationCity + " , " + browseRequestDataModel[indexPath.row].dropoffLocation.dropoffLocationCountry
        cell.item.text = browseRequestDataModel[indexPath.row].item.name
        cell.pickupDate.text = browseRequestDataModel[indexPath.row].pickupDate
        cell.dropoffDate.text = browseRequestDataModel[indexPath.row].dropoffDate

        return cell
    }
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Kunal Malhotra
  • 493
  • 1
  • 5
  • 17
  • From the error and the line, I'd tend to say that you didn't connect the IBOutlet in InterfacBuilder, so `tableView` is nil. – Larme Oct 11 '18 at 15:10
  • https://stackoverflow.com/questions/24948302/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-value https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu etc. – Larme Oct 11 '18 at 15:11
  • It might not be the cause of the issue, but you didn't set `dataSource` or `delegate` of your `tableView`. In `viewDidLoad` you need to do `tableView.dataSource = self` and `tableView.delegate = self` before calling `downloadJson`. – Dávid Pásztor Oct 11 '18 at 15:56
  • @DávidPásztor Now I am getting the same at the tableView.dataSource. Btw I did set the data source and delegate in my storyboard. so I really don't think this is the issue. can you suggest some other solution? – Kunal Malhotra Oct 12 '18 at 07:32
  • In that case it seems that Large was right and you didn't connect your IBOutlet correctly – Dávid Pásztor Oct 12 '18 at 08:09
  • @Larme and DávidPásztor I checked it. All my IBOutlet are connected correctly. Adding a pic of connections inspector in the question please check. – Kunal Malhotra Oct 12 '18 at 08:31
  • Then how is loaded/created `BrowseRequestsViewController`? Because if `tableView` is correctly linked in the `IBOutlet` but causes that crash, I guess that's because the `BrowseRequestsViewController` hasn't be init with the xib/storyboard (so not with the interface link). – Larme Oct 12 '18 at 08:34
  • @Larme You were right. I removed the previous connection and reconnected it now everything is working thanks a lot. – Kunal Malhotra Oct 12 '18 at 08:40
  • @KunalMalhotra this problem occurred when you try copy paste your storyboard and spec ViewController be careful in future. – V D Purohit Oct 12 '18 at 11:43
  • @VDPurohit Yes, I learned it the hard way today. Thank you for the advice. – Kunal Malhotra Oct 12 '18 at 15:20

0 Answers0