1

I'm trying to add an accessory view to an NSAlert with a button on it. When you click on the button in the alert, it should print some text to the console.

The problem is that the application crashes when I don't write the name of the variable that contains the NSViewController of the accessory view, into the completionHandler of the NSAlert. I don't understand why it crashes, the only error I get is this:

EXC_BAD_ACCESS(code=1, address=0x40dedeadbec0)

I don't get it. I don't even use the variable but when it is there, everything works fine. If I comment it, the app crashes.

I got this error in a bigger project and I struggled a lot to find out this was the problem. So I decided to test it in a new empty project and it does the same.

This is my AppDelegate file :

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow!


func applicationDidFinishLaunching(_ aNotification: Notification) {
    // Insert code here to initialize your application
}

func applicationWillTerminate(_ aNotification: Notification) {
    // Insert code here to tear down your application
}

@IBAction func add(_ sender: NSObject) {
    let a = NSAlert()

    let cont = Test()
    a.accessoryView = cont.view
    a.beginSheetModal(for: window, completionHandler: {(modalResponse: NSModalResponse) -> Void in
        //When you comment the line underneath, it doesn't work anymore, why ?
        cont
    })
}

}

This is my Test class file : import Cocoa

class Test: NSViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.
}

@IBAction func some(sender: NSButton) {
    print("something")
}

}

Does anyone know why it behaves like this and how to fix it ?

BoilingFire
  • 191
  • 1
  • 9

0 Answers0