I'm using Swift concurrency in my application. The application creates a window with a toolbar. NSToolbarDelegate
must implement
func toolbar(
_ toolbar: NSToolbar,
itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier,
willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem?
This method has to be nonisolated
, otherwise you get a warning
Main actor-isolated instance method 'toolbar(_:itemForItemIdentifier:willBeInsertedIntoToolbar:)' cannot be used to satisfy nonisolated protocol requirement
This method is designed to create a new NSToolbarItem
. Apple docs:
Use this method to create new NSToolbarItem objects when the toolbar asks for them.
However, NSToolbarItem
is isolated on @MainActor
, so you have to call its constructor on @MainActor
, which is impossible to do from a nonisolated
method and still return a value from that method.
Is this a bug in the current version of AppKit or is there a workaround?
Most Apple frameworks, and that includes AppKit, have not been audited for concurrency. So you run into problems where methods like toolbar(_:itemForItemIdentifier:willBeInsertedIntoToolbar:)
, which is obviously meant to be a main-actor-only thing, confuse the compiler.
Standard practice right now is to import such frameworks using the @preconcurrency
attribute.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"