如何制作`nsviewController` a@mainactor`并实现nsmenuitemvalidation“ a@mainactor”

发布于 2025-01-25 13:06:20 字数 677 浏览 0 评论 0原文

我有一个自定义nsviewController实现nsmenuitemvalidation协议的子类。这很好,直到我将视图控制器标记为@mainactor

我认为苹果将所有视图控制器都标记为主要演员已经隔离了,但也许只能在iOS上这样做。但是我认为标记我的子类是安全的。

一旦添加@MainActor行,我就会获得警告>实例方法“ validatemenuitem”隔离到全局actor'mainactor'无法满足协议'nsmenuitemvalidation':

@MainActor
final class MyViewController: NSViewController, NSMenuItemValidation {
    func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
    }
}

IS IS 的相应要求。标记自定义nsviewController子类是@MainActor是不是可以的吗?还是我应该如何处理此警告?

我想标记我的视图控制器,因为它需要与(明确)隔离到主要演员的其他类别,我想避免完全不是必需的异步边界,知道视图控制器将始终运行主线程。

I've a custom NSViewController subclass that implements the NSMenuItemValidation protocol. That works just fine, until I mark the view controller as @MainActor.

I believed Apple marked all view controllers as main actor isolated already, but maybe it does so only on iOS. But I thought it would be safe to mark my subclass as such.

Once I add the @MainActor line, I get the warning Instance method 'validateMenuItem' isolated to global actor 'MainActor' can not satisfy corresponding requirement from protocol 'NSMenuItemValidation':

@MainActor
final class MyViewController: NSViewController, NSMenuItemValidation {
    func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
    }
}

Is it not okay to mark a custom NSViewController subclass as @MainActor? Or how should I deal with this warning?

I want to mark my view controller, because it needs to co-operate with other classes that are (explicitly) isolated to the main actor and I want to avoid various async boundaries that are totally not necessary, knowing the view controller will always run on the main thread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美羊羊 2025-02-01 13:06:20

您必须标记带有非分离的函数。我假设这是因为NSMENUITEMVALIDATION协议不限于主队列。

非分离的func dastemenuitem(_ benuitem:nsmenuitem) - > bool {}

You must mark the function w/ nonisolated. I'm assuming this is because the NSMenuItemValidation protocol is not restricted to the main queue.

nonisolated func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文