Reputation: 43
I am using a main file and an AppDelegate with the code below. The issue occurs when I try to quit my app from the Dock. When I right-click on the app icon and choose "Quit", it crashes with the error "Thread 1: signal SIGTERM"
at the line nsApplication.run()
.
Although I have implemented deinit, applicationShouldTerminate, and applicationWillTerminate, none of them print anything to the Xcode console when the crash happens. However, sometimes, completely at random, the app quits correctly using the Quit option from the Dock, and I see the expected prints. Most of the time, though, the app crashes when trying to quit. My ContentView is just a simple "Hello, World!" view. How can I solve this issue?
import Cocoa
let nsApplication: NSApplication = NSApplication.shared
let appDelegate: AppDelegate = AppDelegate()
nsApplication.delegate = appDelegate
nsApplication.run()
import Cocoa
import SwiftUI
class AppDelegate: NSObject, NSApplicationDelegate {
private lazy var window: NSWindow = NSWindow()
deinit {
print("AppDelegate is being deallocated")
}
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
print("terminateNow")
return .terminateNow
}
func applicationWillTerminate(_ notification: Notification) {
print("Application will terminate")
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
window.styleMask = [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView]
window.backingType = .buffered
window.isReleasedWhenClosed = false
window.center()
window.setFrameAutosaveName("Main Window")
window.title = "Window"
window.isMovableByWindowBackground = true
window.contentView = NSHostingView(rootView: ContentView())
window.makeKeyAndOrderFront(nil)
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
}
.frame(width: 400.0, height: 200.0)
}
}
Upvotes: 0
Views: 83