macOS main.swift and Main actor-isolated conformance cannot be used in nonisolated context

For a simple, resourceless cocoa apps I used to manually setup the application lifecycle (mimicking what's documented here: https://aninterestingwebsite.com/documentation/appkit/nsapplication), so my main.swift would look like:

import Cocoa

let delegate = SomeDelegate()
_ = NSApplication.shared
NSApp.delegate = delegate
NSApp.run()

This triggers a warning in Xcode 26.2: "Main actor-isolated conformance of SomeDelegate cannot be used in nonisolated context; this is an error in Swift 6 language mode".

so what is the recommended way to refactor above so that it is Swift 6 compliant?

Answered by DTS Engineer in 885419022

I got this working as following:

  1. Using Xcode 26.4, I created a new project from the macOS > App template, choosing Storyboard for the UI.
  2. I removed @main from AppDelegate.
  3. I added a main.swift file like so:
import Cocoa

func main() {
    let delegate = AppDelegate()
    _ = NSApplication.shared
    NSApp.delegate = delegate
    NSApp.run()
}

main()

The difference is that I put the code into a main() function. That function name isn’t special — you could just as easily call it foo() — but it’s important that the code not be at the top level. Top-level code is weird in Swift (-:

Having said that, I recommend against doing this. Rather, just call NSApplicationMain. Note that the doc you referenced says “which is functionally similar to”, not “is implement as”. The actual implementation of NSApplicationMain is very different, and by not calling it you’re taking yourself far off the beaten path.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

I got this working as following:

  1. Using Xcode 26.4, I created a new project from the macOS > App template, choosing Storyboard for the UI.
  2. I removed @main from AppDelegate.
  3. I added a main.swift file like so:
import Cocoa

func main() {
    let delegate = AppDelegate()
    _ = NSApplication.shared
    NSApp.delegate = delegate
    NSApp.run()
}

main()

The difference is that I put the code into a main() function. That function name isn’t special — you could just as easily call it foo() — but it’s important that the code not be at the top level. Top-level code is weird in Swift (-:

Having said that, I recommend against doing this. Rather, just call NSApplicationMain. Note that the doc you referenced says “which is functionally similar to”, not “is implement as”. The actual implementation of NSApplicationMain is very different, and by not calling it you’re taking yourself far off the beaten path.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

macOS main.swift and Main actor-isolated conformance cannot be used in nonisolated context
 
 
Q