I have a question about exception handling in Swift. The UIKit documentation for the UIStoryboard class states that the instantiateViewControllerWithIdentifier (identifier: String) -> UIViewController function throws an exception if the identifier is zero or does not exist in the storyboard. However, if I use do / try / catch as shown below, I get the warning "There are no calls to metal functions in the try expression."
This is just a warning, so I thought it was a problem with intellisense; but when I run the following code and intentionally use an invalid identifier, an exception will not be detected and SIGABRT is thrown.
let storyboard = UIStoryboard.init(name: "Main", bundle: nil) do { let controller = try storyboard.instantiateViewControllerWithIdentifier("SearchPopup") // This code is only included for completeness... controller.modalPresentationStyle = .Popover if let secPopoverPresentationController = controller.popoverPresentationController { secPopoverPresentationController.sourceView = self.view secPopoverPresentationController.permittedArrowDirections = .Any secPopoverPresentationController.barButtonItem = self.bSearchButton } self.presentViewController(controller, animated: true, completion: nil) // End code included for completeness. } catch { NSLog( "Exception thrown instantiating view controller." ); return; }
How should you do / try / catch for functions that throw such exceptions?
Thanks in advance.
Brian
Bryon source share