terminating with uncaught exception of type NSException

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let alert = SCLAlertView()
        alert.addButton("A",target:self,selector: #selector(FirstViewController.firstButton))
        alert.addButton("B",target:self,selector: #selector(FirstViewController.secondButton))
        self.dismiss(animated: true, completion: {
            alert.showSuccess(kSuccessTitle,subTitle: kSubtitle1)
//            self.present(secondViewController,animated: true,completion: nil)
        })
    }
    @objc func firstButton(info: [UIImagePickerController.InfoKey : Any])
    {
        let sb = UIStoryboard(name:"Main",bundle: Bundle.main)
        let secondViewController = sb.instantiateViewController(withIdentifier: "view2") as! SecondViewController
        secondViewController.infoFromViewOne = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
        secondViewController.flag = 0
        self.present(secondViewController,animated: true,completion: nil)
    }
    @objc func secondButton()
    {
        let sb = UIStoryboard(name:"Main",bundle: Bundle.main)
        let thirdViewcontroller = sb.instantiateViewController(withIdentifier: "SecondViewController") as! ViewController
        self.present(thirdViewcontroller,animated: true,completion: nil)
    }

This is my code,

My desire result is that when I click the first button, the page jump to "SecondViewcontroller" and the image in the SecondViewcontroller's imageView is the photo I just select.


But now , after selecting the photo and click the first button, the app crashes at AppDelegate.swift, with error message:

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


What should I do to make it correct?


Thanks in advance.

Answered by OOPer in 356561022

I wrote you need to change the `firstButton` method to:

    @objc func firstButton() { 


But you still keep the method as `firstButton(info: [UIImagePickerController.InfoKey : Any])`, that would never work...

Objective-C is throwing an exception. There will probably be an explanation on the console as well as a stack trace.

I'm sure there was no explanation on the console, there's just a line message.

Usually `terminating with uncaught exception of type NSException` shows some messages in the debug console. Are you really watching the debug console? Aren't you just watching the error message displayed in the editor?


One more, usually an action method like `firstButton` cannot take an argument like `(info: [UIImagePickerController.InfoKey : Any])`. And as far as I check the source code of `SCLAlertView`, it does not pass any useful information when calling its action methods.

You might expect that `info` in your `imagePickerController(_:didFinishPickingMediaWithInfo:)` would be passed to `firstButton(info:)`, but such sort of things will never happen. It is the framework you use that defines what is passed to an action method, not your expectation.


Remove the formal parameters of your `firstButton` as:

    @objc func firstButton()

and pass `info` in another way.


---

By the way, you put ID "view2" for your `SecondViewController` and "SecondViewController" for `ViewController` which is actually `thirdViewController`? Better avoid such confusing naming even in an experimental project.

Thank you for your advice, and then I'll correct the wrong programming habits.

I'm certain that there is only one line "libc++abi.dylib: terminating with uncaught exception of type NSException" at the debug console,and I don't know how to pass 'info' in another way.


Ive tried multiple variations of this, but none of them seem to work.

Thanks for reporting.


As I cannot run your app with my Xcode, please leave the debug console thing aside, sometimes Xcode and Swift runtime may show unusual behavior or you or I may be missing something.


I don't know how to pass 'info' in another way.

For example, you can use instance property to pass something from a method to another method of the same class.


    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        self.passingImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
        let alert = SCLAlertView()
        alert.addButton("A",target:self,selector: #selector(FirstViewController.firstButton))
        alert.addButton("B",target:self,selector: #selector(FirstViewController.secondButton))
        self.dismiss(animated: true, completion: {
            alert.showSuccess(kSuccessTitle,subTitle: kSubtitle1)
        })
    }
    
    private var passingImage: UIImage?
    
    @objc func firstButton() {
        let sb = UIStoryboard(name:"Main",bundle: Bundle.main)
        let secondViewController = sb.instantiateViewController(withIdentifier: "view2") as! SecondViewController
        secondViewController.infoFromViewOne = self.passingImage
        secondViewController.flag = 0
        self.present(secondViewController,animated: true,completion: nil)
    }

I haven't (and cannot) check if you need some other fixes or not, please tell me when you find something wrong with the code above.

How is SCLAlertView defined ?


Did you notice that :


UIAlertView
is deprecated in iOS 8. (Note that
UIAlertViewDelegate
is also deprecated.) To create and manage alerts in iOS 8 and later, instead use
UIAlertController
with a
preferredStyle
of
UIAlertController.Style.alert
.


Then you can pass info through the action handler, with the following pattern :

let alertController = UIAlertController(
            title: "Alert title"),
            message: "Do what you have to do")

let okAction = UIAlertAction(title: "Alert", style: .default, handler:  { (action) -> Void in
            // you can use here any info you need to pass
          )

 alertController.addAction(okAction)

I added the code above and run the project. As soon as I click the first button.The app crashes at AppDelegate.swift at the line:

class AppDelegate: UIResponder, UIApplicationDelegate {

With error message "Thread 1: signal SIGABRT"

And it still does the same in debug console.

With error message "Thread 1: signal SIGABRT"

And it still does the same in debug console.


Sorry, that does not provide any info I can use to help you. Please find some info indicating what's happening with any debugging fieature of Xcode.

You can easily find the source code of SCLAlertView on the web. And it has nothing to do with `UIAlertView`.

I tried adding a breakpoint within the method "firstButton" and I found the method is never called.

So then I added a breakpoint at the method "didFinishPickingMediaWithInfo" . after running through the code in the method, the app crashes.

On which line have you added breakpoint? And going step by step, on which line your app crashed?

I added breakpoint at the first line of the method "didFinishPickingMediaWithInfo" and after passing through the code,it crashes:

self.dismiss(animated: true, completion: {

What happens if you comment out the lines inside the completion handler?

        self.dismiss(animated: true, completion: {
//            alert.showSuccess(kSuccessTitle,subTitle: kSubtitle1)
        })

After commenting the line, nothing happened when the photo is selected. It just back to the original page.


Then you have something wrong with the line `alert.showSuccess(kSuccessTitle,subTitle: kSubtitle1)`.


What happens if you revert the method to the original:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
//        self.passingImage = info[.editedImage] as? UIImage
        let alert = SCLAlertView()
        alert.addButton("A",target:self,selector: #selector(FirstViewController.firstButton))
        alert.addButton("B",target:self,selector: #selector(FirstViewController.secondButton))
        self.dismiss(animated: true, completion: {
            alert.showSuccess(kSuccessTitle,subTitle: kSubtitle1)
        })
    }
terminating with uncaught exception of type NSException
 
 
Q