-1

I'm trying to put the following in a do try catch block, but I always end up getting a coding error. Can someone help me?

 let here = CGRect(x: UIScreen.main.bounds.width - 30, y: 80, width: 10, height: 10)
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            let popTip = PopTip()
            popTip.show(text: "Font Sizes", direction: .down, maxWidth: 200, in: (self.navigationController?.view)! , from: here, duration: 3)
        }
ibyte
  • 463
  • 4
  • 17
  • 3
    Only APIs which `throw` can be `caught`. Handle the optional properly. – vadian Jan 02 '21 at 14:25
  • I get an error at the popTip.Show line. It's because the navigationController is not available because of the dispatch and the user moves to another controller before the async task is executed after 3 seconds of delay. How do I go about handling it then? I'm sorry for being a trouble. I've spent hours trying to figure out. I finally gave up. – ibyte Jan 02 '21 at 14:54

1 Answers1

1

Your problem is that you have nothing that actually throws an error (i.e. marked with try). I can't fully understand what you are saying in your comment, however I believe you might solve this without a do-catch clause but with a guard-let statement to check if self.navigationController.view is nil. If it's nil, just return the function.

Luca Sfragara
  • 624
  • 4
  • 16
  • 1
    That worked like a charm. I'm a self-taught programmer for the most part, but I owe my success to co-programmers on StackOverflow. Thanks a lot. – ibyte Jan 03 '21 at 10:42
  • 1
    Always a pleasure to help a self-taught programmer like me!! – Luca Sfragara Jan 03 '21 at 12:15