-3

只是@MainActor语法糖DispatchQueue.main.async还是有其他用途?

4

1 回答 1

1

如果我们看一下 and 的声明UILabelUIViewController我们可以看到它们都被新@MainActor属性注解了:

@MainActor class UILabel: UIView
@MainActor class UIViewController: UIResponder

这意味着,当使用 Swift 的新并发系统时,这些类(以及它们的任何子类)上的所有属性和方法将自动在主队列上设置、调用和访问。所有这些调用都将自动通过系统提供的路由,该系统MainActor始终在主线程上执行所有工作——完全消除了我们手动调用的需要DispatchQueue.main.async

来源:https ://www.swiftbysundell.com/articles/the-main-actor-attribute/

于 2021-12-22T09:54:23.940 回答