-1

When I try to reload tableview I get an error like

libc++abi.dylib: terminating with uncaught exception of type NSException

My UITableViewController

class NewTableViewController: UITableViewController {

var videos = [Video]()
    {
    didSet {

            self.tableView.reloadData() //error here
    }
}
let URL = "https://api.vid.me/channel/1/new"

override func viewDidLoad() {
    super.viewDidLoad()

    let request = Alamofire.request(.GET, self.URL)
        request.responseObject { (response: Response<NewestVideos, NSError>) in

        switch response.result {
        case .Success(let newest):
            self.videos = newest.videos!
        case .Failure(let error):
            print("Request failed with error: \(error)")
        }
    }
}

override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return self.videos.count}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NewTableViewCell
    let video = videos[indexPath.row]
    cell.newVideoNameLabel?.text = video.completeUrl != nil ? video.completeUrl! : "nil"
     return cell
     }
}

I think it's problem with threading? In my app I make a .get request and I get a value if I try to print in didSet like print(self.videos) I tried adding

dispatch_async(dispatch_get_main_queue()) {
   self.tableView.reloadData()
}
jose920405
  • 7,982
  • 6
  • 45
  • 71
DmitrievichR
  • 119
  • 1
  • 11

2 Answers2

1

Make sure when you set videos, you are calling it in the Main Queue

You can protect videos when you set in on Main Queue:

dispatch_async(dispatch_get_main_queue()) {
   self.videos = newest.videos!
}

Or you can protect it under:

didSet {
    dispatch_async(dispatch_get_main_queue()) {
        self.tableView.reloadData()
    }
}
E-Riddie
  • 14,660
  • 7
  • 52
  • 74
  • @DmitrievichR Not in get, because you want to reload the data when the var is set (didSet) – E-Riddie Sep 12 '16 at 13:48
  • oh not get{} i mean didSet{} but it does not matter i get crash now too when `var videos = [Video](){ didSet { dispatch_async(dispatch_get_main_queue()) { self.tableView.reloadData() } } }` – DmitrievichR Sep 12 '16 at 13:59
  • Then you can check the [jose920405's answer](http://stackoverflow.com/a/39450885/2664437) if you have set the outlet correctly. Check your IB – E-Riddie Sep 12 '16 at 14:01
1

The libc++abi.dylib: terminating with uncaught exception of type NSException error, happens from any ways.

  • Remove reference from outlet
  • Bad name in performSegueWithIdentifier
  • IBActions type.

Check this link for more info

Community
  • 1
  • 1
jose920405
  • 7,982
  • 6
  • 45
  • 71
  • There's one more way this error can happen, for me it was when I overlooked something in this call `let cancelAction = UIAlertAction(title: nil, style: .Cancel) { action in ...` . Even that the `title` parameter is of type `String?` you should not use `nil` (when running Xcode 8 and Swift 2.3 legacy mode). Something is `nil` when in actually shouldn't be. Happy hunting. Since Xcode 8 this error message has been the most confusing to me. The working code for my example is to use an actual `String`: `let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { action in ...` – Andrej Sep 17 '16 at 16:14