1

On the click of a create button, I navigate to tableviewcontroller screen. But nothing is populated in the tableview since the array which populates the tableview hasn't been called yet.

Now once the tableview screen is reached, after a few seconds another method is called elsewhere(in another file), which in turn calls a function in this tableviewcontroller screen. This is that method of the tableviewcontroller screen which is called...

  func stopIndicator(thegrpName: String) {
    stopIndicator()

    let realm = try! Realm()
    let chatGrp = realm.objects(ChatGroup.self)
    chatGroup = chatGrp

    tableview.reloadData() //CRASH HAPPENS HERE

  }

In this method, once I reach tableview.reloadData() it crashes with the error Unexpectedly found nil while unwrapping an optional value..

I referred this link which seems to have a similar problem...but couldn't get much help out of it...

What could be the reason for this crash...?

EDIT 1: The numberOfRows and cellForRowAt.. is given like so...

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if let chatGrp = chatGroup {
      return chatGrp.count
    }
    return 0
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: ChatGroupTableViewCell = tableView.dequeueReusableCell(withIdentifier: "chatgroupIdentifier") as! ChatGroupTableViewCell

    let groupChatObj = chatGroup![indexPath.row]
    cell.chatLabel.text = groupChatObj.lastMessage?.text?.text
    return cell
  }
user308123
  • 135
  • 1
  • 12
  • Please show your `numberOfRowsInSection` and `cellForRowAt` methods. Also see [this](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) for general troubleshooting and advice for this exception – Paulw11 Nov 23 '18 at 05:39
  • Seems `tableview ` is `nil`, check the outlet for connection if it's added in a storyboard. – Satish Nov 23 '18 at 05:43
  • checked that @Satish..its connected properly.. – user308123 Nov 23 '18 at 05:44
  • Have edited the question with noOfRows & cellForRowAt @Paulw11 ... – user308123 Nov 23 '18 at 05:45
  • 2
    Set an exception breakpoint to see which line is crashing. – Paulw11 Nov 23 '18 at 05:57
  • The link I mentioned in the question seems to identify the problem correctly @Paulw11..But I couldn't find the actual cause based on that for my specific issue..Also the reason seems to be because awakeFromNib isn't called... – user308123 Nov 23 '18 at 06:05
  • 1
    Instead of using exclamation marks ("!") use optional unwrapping, and print something if the value you are trying to unwrap is nil. That way, you'll know exactly which value is nil, instead of having to guess. –  Nov 23 '18 at 06:08
  • “in another file”? Describe it more and add code where you are getting reference for this TableView from your ViewController to another file – Robert Dresler Nov 23 '18 at 06:11
  • I'm creating a chat room using xmpp @RobertDresler So the delegate method that gets called after a room creation happens after I have navigated to the tableview screen. That delegate method is given in a different file. From that delegate method, the function `func stopIndicator(theGrp....` in the tableview screen is called... – user308123 Nov 23 '18 at 06:17
  • Also @RobertDresler from the other file the function in tableview screen is called like so.....`ChatGroupMessageViewController.shared.stopIndicator(thegrpName: name)` – user308123 Nov 23 '18 at 06:18
  • Could you post a picture for `stack trace` after crash ? – AechoLiu Nov 23 '18 at 07:07
  • stack trace as in @AechoLiu ...? – user308123 Nov 23 '18 at 07:16
  • [An example like this link.](https://stackoverflow.com/q/11747802/419348). The crash will give you a stack trace info. It can help developer to find out what's wrong. – AechoLiu Nov 23 '18 at 08:50

1 Answers1

0

It looks you are trying to create delegate method but in another file where you are trying to call delegate method stopIndicator you are calling method on singleton instead which gives you an error.

So, set delegate right. First create protocol

protocol YourProtocol {
    func stopIndicator(thegrpName: String)
}

then in another file create delegate property

var delegate: YourProtocol?

now when you need to call delegate method stopIndicator call this

delegate?.stopIndicator(thegrpName: ...)

now add to your ViewController protocol

ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, YourProtocol

and now somewhere set your another file class delegate as your ViewController (if its view set it in viewDidLoad if it is another ViewController set it in prepareForSegue)

fileClass.delegate = self 
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40