3 day I can not understand what the problem is, the code seems to be correct, everything seems to be parsed correctly, but it still gives an error

class MainViewController: UIViewController, UITableViewDataSource { @IBOutlet weak var NameLabel: UILabel! @IBOutlet weak var LogoPicture: UIImageView! let urlString = "https//my/ip.com" @IBOutlet weak var tableView: UITableView! var nameArray = [String]() var timeArray = [NSNumber]() var logoArray = [String]() override func viewDidLoad() { super.viewDidLoad() let defaults = UserDefaults.standard if let name = defaults.string(forKey: "NameText"){ NameLabel.text = name } if let logo = defaults.string(forKey: "LogoText"){ let strurl = NSURL(string: logo) let dtinternet = NSData(contentsOf:strurl! as URL) LogoPicture.image = UIImage(data: dtinternet as! Data) self.view.addSubview(LogoPicture)} self.downloadJsonWithURL() } @IBAction func ExitButton(_ sender: Any) { UserDefaults.standard.set(false, forKey: "isUserLoggedIn"); UserDefaults.standard.synchronize(); } func downloadJsonWithURL(){ let url = NSURL(string: urlString) URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary { //print(jsonObj!.value(forKey: "data")) if let dataArray = jsonObj!.value(forKey: "data") as? NSArray { for datata in dataArray { if let dataDict = datata as? NSDictionary { if let created = dataDict.value(forKey: "created") { self.timeArray.append(created as! NSNumber) } if let authorArray = dataArray.value(forKey: "author") as? NSArray { for datata1 in authorArray { if let authorDict = datata1 as? NSDictionary { if let namea = authorDict.value(forKey: "name") { self.nameArray.append(namea as! String) } if let logo = authorDict.value(forKey: "logo") { self.logoArray.append(logo as! String) } } } } //print(self.nameArray) OperationQueue.main.addOperation({self.tableView.reloadData()}) } } } } }).resume() } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return nameArray.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell cell.nameView.text = nameArray[indexPath.row] let date = NSDate(timeIntervalSince1970: TimeInterval(timeArray[indexPath.row])) let dateFormatter = DateFormatter() dateFormatter.timeZone = TimeZone(abbreviation: "GMT") //Set timezone that you want dateFormatter.locale = NSLocale.current dateFormatter.dateFormat = "yyyy-MM-dd HH:mm" cell.timeView.text = dateFormatter.string(from: date as Date) print(timeArray[indexPath.row]) let imgURL = NSURL(string: logoArray[indexPath.row]) if imgURL != nil { let data = NSData(contentsOf: imgURL as! URL) cell.logoView.image = UIImage(data: data as! Data) } return cell } } 

Here is the error fatal error: unexpectedly found nil while unwrapping an Optional value

here in this line let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell . please tell me what i am doing wrong

    1 answer 1

    check if the data comes in, if yes then check the bundles in the TableViewCell

    • This error arrived was return nameArray.count, the element for this string was incorrectly selected. TimeArray was necessary because nameArray gave the wrong number - Sergey